01 November 2009

Dependency property

Windows Presentation Foundation (WPF) provides a set of services that can be used to extend the functionality of a common language runtime (CLR) property. Collectively, these services are typically referred to as the WPF property system. A property that is backed by the WPF property system is known as a dependency property.
To be more clear this is same as .NET normal property system, regardless it can hold default value with built in validation sytem.
Intrduction of dependency property, Microsoft has achieved following things.
Value Inheritance
Nothing but the ability of a .NET element to inherit the value. If no local value is set, the dependency property navigates up the logical tree until it finds a value.to be more clear I have root element and have 5 text box on that, and if i say the font defanition on root level all text box will take up that font, unless it is not defined at text box lev el.
Reduced memmory
Yes.It is going to save lot of memory.For example I am defining a button, it has round about 100 properties asssociated with this.Dependency property enable us not to define this 100 properties at element level as it has the capability of inherit the value from parent levels.
Change notification
Dependency properties have a built-in change notification mechanism.


new FrameworkPropertyMetadata( string,
OntextPropertyChanged,
OnCoerceTextProperty ),
OnValidateTextProperty );


Please see the example:


public static readonly DependencyProperty IsSpinningProperty =
DependencyProperty.Register(
"IsSpinning", typeof(Boolean),


...


);
public bool IsSpinning
{
get { return (bool)GetValue(IsSpinningProperty); }
set { SetValue(IsSpinningProperty, value); }
}

06 March 2009

Populating WPF TreeView with xml file in c#?

This wil help you to populate a Wpf UI control form a complex hielerichal xml file

recursive call for adding nodes
-----------------------------------
private void addTreeNode(XmlNode xmlNode, TreeViewItem treeNode)
{
XmlNode xNode;
TreeViewItem tNode;
XmlNodeList xNodeList;
if (xmlNode.HasChildNodes) //The current node has children
{
xNodeList = xmlNode.ChildNodes;
for (int x = 0; x <= xNodeList.Count - 1; x++)
//Loop through the child nodes
{
xNode = xmlNode.ChildNodes[x];
int i = treeNode.Items.Add(new TreeViewItem() { Header = xNode.Name });
tNode = treeNode.Items[x] as TreeViewItem;
addTreeNode(xNode, tNode);
}
}
else //No children, so add the outer xml (trimming off whitespace)
treeNode.Header = xmlNode.OuterXml.Trim();
}

-------------------------------------------------------------------------------------
try
{
//Just a good practice -- change the cursor to a
//wait cursor while the nodes populate
this.Cursor = Cursors.Wait;
//First, we'll load the Xml document
XmlDocument xDoc = new XmlDocument();
xDoc.Load(dlg.FileName);
//Now, clear out the treeview,
//and add the first (root) node
myTreeView.Items.Clear();
TreeViewItem nodeToAdd = new TreeViewItem();
nodeToAdd.Header = xDoc.DocumentElement.Name;
myTreeView.Items.Add(nodeToAdd);
TreeViewItem tNode = new TreeViewItem();
tNode = (TreeViewItem)myTreeView.Items[0];
//We make a call to addTreeNode,
//where we'll add all of our nodes
addTreeNode(xDoc.DocumentElement, tNode);
foreach (TreeViewItem item in myTreeView.Items)
{
item.IsExpanded = true;
}
this.Cursor = Cursors.Arrow;
}
catch
{
throw;
}