Data Converstion and Task Addition with WPF - Adding Tasks, Continued
(Page 4 of 4 )
Now that the dialog is set up, it's time to make it all work. This is actually really simple. Back in Window1, either double click the "Add Task" button in the designer to create an event handler, or add it manually through XAML:
<Button Name="AddButton" Grid.Row="1" Grid.Column="0" Margin="0,3,2,0" Content="Add Task" Click="AddButton_Click" />
In the event handler, we need to create an instance of AddTaskDialog and call it as a dialog using the ShowDialog method. This will return a boolean value like a normal dialog does. If True is returned (that is, if the user gives the application the okay to actually add a new task), then a task needs to be added to the ListBox.
To do this, we need to convert all of the data into an XML element (represented by the XmlElement class) and stick it into the XmlDataProvider. Open up the code-behind for Window1 (Window1.xaml.cs). We're going to be using various classes in the System.Xml namespace, so go ahead and add a using directive:
using System.Xml;
Most of the code relates to XML rather than WPF, so there's no need to explain anything, except for one method in bold:
private void AddButton_Click(object sender, RoutedEventArgs e)
{
AddTaskDialog dlg = new AddTaskDialog();
if (dlg.ShowDialog() == true)
{
// Get the document
XmlDocument document = ((XmlDataProvider)FindResource("tasks")).Document;
// Create the Task element
XmlElement task = document.CreateElement("Task");
// Create the Name element
XmlElement name = document.CreateElement("Name");
name.InnerText = dlg.TaskTitle.Text;
task.AppendChild(name);
// Create the Priority element
XmlElement priority = document.CreateElement("Priority");
priority.InnerText = dlg.TaskPriority.Text;
task.AppendChild(priority);
// Create the Done element
XmlElement done = document.CreateElement("Done");
done.InnerText = "No";
task.AppendChild(done);
// Create the Description element
XmlElement description = document.CreateElement("Description");
description.InnerText = dlg.TaskDescription.Text;
task.AppendChild(description);
document.DocumentElement.AppendChild(task);
}
}
The FindResource method does as its name implies: it obtains a reference to a resource defined in XAML. Note that the result must then be cast into the proper type.
Next week, we will finish building our application. You won't want to miss it.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |