In one project we changed the design for WPF binding. Earlier we used to bind the CLR properties for most of the controls but we came up with XMLDataProvider in later stage. That is where I learnt XMLDataProvider and XPath.
<XmlDataProvider x:Key="PropertyData" IsInitialLoadEnabled="True" IsAsynchronous="False" XPath="ImagineTypeSystem">
<x:XData>
<ImagineTypeSystem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="">
<Property ID="Type">TextBox</Property>
<Property ID="Text">
<Text>
<Property ID="Text">ABCDE</Property>
<Property ID="TextColor">
<SolidColorBrush>
<Property ID="Color">
<Color>
<Property ID="Value">#000000</Property>
<Property ID="Transparency">50</Property>
</Color>
</Property>
</SolidColorBrush>
</Property>
<Property ID="ShadowColor">
<Color>
<Property ID="Value">#FF0000</Property>
<Property ID="Transparency">50</Property>
</Color>
</Property>
</Text>
</Property>
</ImagineTypeSystem>
</x:XData>
</XmlDataProvider>
</ResourceDictionary>
If you consider this example we have added an XMLDataProvider object in the Resource Dictionary.
- Data island should be covered by X:XData tag.
- x:Key is used to identify the XMLDataProvider.
- By Default IsInitialLoadEnabled is set to True. If we don't want to run on load, we can set it to false.
- If IsAsynchronous is set to true, it will run in worker thread. By default it is set to false.
- XPath attribute will tell the path to be queried to get data collection.
Now I have a textbox control whose properties has to be set to above XMLDataProvider.
<TextBox>
<TextBox.Text>
<Binding XPath="/ImagineTypeSystem/Property[@ID='Text']/Text/Property[@ID='Text']" Path="InnerXml" FallbackValue="{StaticResource ResourceKey=DefaultCaption}"/>
</TextBox.Text>
</TextBox>
Above code shows Text property of Textbox is set to Text Property in the XMLDataProvider.
Now Consider the XPath "/ImagineTypeSystem/Property[@ID='Text']/Text/Property[@ID='Text']"
XPath can be given like URLs. XPath uses certain syntax.
/ Indicate the Immediate child has to be selected.
@ Indicates attribute name.
You can check this link for more operators and special characters.
http://msdn.microsoft.com/en-us/library/ms256122(v=vs.110).aspx
Some of the mistakes I did while learning Path
- I didn't give XPath="ImagineTypeSystem" in XMLDataProvider tag. Then I didn't get the output. It came blank.
- If you don't put xmlns="" in root node, then also output will be blank. That is because it will be querying System.Windows instead of data island. We have declared xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation". To clear namespace we have to add xmlns="" to root node.
- Consider XPath = "/ImagineTypeSystem/Property[@ID='Text']/" and you don't put Path="InnerXML", then you will get the output as ABCDE#00000050#FF000050 . Only we will get the values of inner node.
- To get the whole Inner Node we have to put Path="InnerXML" then we will get below output.
<Text><Property ID="Text">ABCDE</Property><Property ID="TextColor"><SolidColorBrush><Property ID="Color"><Color><Property ID="Value">#000000</Property><Property ID="Transparency">50</Property></Color></Property></SolidColorBrush></Property><Property ID="ShadowColor"><Color><Property ID="Value">#FF0000</Property><Property ID="Transparency">50</Property></Color></Property></Text>
Some of the link with more information :
http://www.codeproject.com/Articles/26875/WPF-XmlDataProvider-Two-Way-Data-Binding
http://www.w3.org/TR/xpath/
http://www.beansoftware.com/ASP.NET-Tutorials/XMLDataSource-Control.aspx
Thanks to Sudarshan, who was lead in Schneider who put me into this learning.
No comments:
Post a Comment