Tuesday, October 27, 2009

How to use DataTemplateSelector in WPF ContentControl

DataTemplateSelector provides a way to choose a data template based on the data object and the data-bound element.

Steps to implement DataTemplateSelector in ContentControl.

1. Create a class that inherits DataTemplateSelector.


2. Implement method SelectTemplate.

public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
code here
}

3. In your XAML under resource, declare all data templates you wish to as example below.



4. Initialize your datatemplate selector class in reference to step number 1.



5. Under ContentControl:
ContentTemplateSelector="{StaticResource xMyTemplateSelector}"/>

Thursday, October 22, 2009

Syntax for Deleting Record to XML using LINQ

Steps in Editing an XML using LINQ and C#

1. Get the path of your XML file.

string fullXmlPath =
System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "file name.xml";

2. Load the path.

XElement LogXmlDocument = XElement.Load(fullXmlPath);

3. Prepare the data.

var navResultsPlant = LogXmlDocument.Descendants().Where(
e => e.Attribute("id").Value == id).FirstOrDefault();
if (navResultsPlant != null)
navResultsPlant.Remove();

4. Save.

LogXmlDocument.Save(fullXmlPath);

Thursday, October 15, 2009

Caliburn: An Application Framework for WPF

Today, I've learned how to use caliburn framework in Model-View-View Model(MVVM) designed pattern.
Without using the application framework, you can set your event in your view.xaml by this:



in your view.cs, you need to hard code this:
void MouseEnter(object sender, MouseEventArgs e)
{
//code here
}

using the implementation above, it breaks the rule of MVVM, the view classes have no idea that the model classes exist, while the ViewModel and model are unaware of the view.

but using caliburn, you need to add this to your view.xaml by



in your viewmodel.cs,
public void OnMouseEnter(MouseEventArgs e)
{
//code here
}

Thursday, October 8, 2009

Syntax for Editing Record to XML using LINQ and C#

Steps in Editing an XML using LINQ and C#

1. Get the path of your XML file.

string fullXmlPath =
System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "file name.xml";

2. Load the path.

XElement LogXmlDocument = XElement.Load(fullXmlPath);

3. Prepare the data.
a. Editing an Attribure

XElement propResults = LogXmlDocument.Elements("Property").Where(
e => e.Attribute("id").Value == id).FirstOrDefault();
propResults.SetAttributeValue("Description", desc);
propResults.SetAttributeValue("Manufacturer", manufac);

b. Editing an Element

XElement propResults = LogXmlDocument.Elements("Property").Where(
e => e.Attribute("id").Value == id).FirstOrDefault();
propResults.SetElementValue("Manufacturer", manufacturer);
propResults.SetElementValue("ModelNumber", modelnumber);
propResults.SetElementValue("Type", type);

4. Save.

LogXmlDocument.Save(fullXmlPath);

Thursday, October 1, 2009

Syntax for Adding Record to XML using LINQ

XML Hierarchy




Steps in Adding

1. Get the path of your XML file.

string fullXmlPath =
System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "file name.xml";

2. Load the path.

XElement LogXmlDocument = XElement.Load(fullXmlPath);

3. Prepare the data.

LogXmlDocument.Add
(new XElement
("Property",
new XAttribute("id", newid),
new XAttribute("iid", sid),
new XElement("Identification", id),
new XElement("Description", desc),
new XElement("Manufacturer", manufac)
);

4. Save.

LogXmlDocument.Save(fullXmlPath);

About Me

My photo
simple and straight forward