views:

17

answers:

1

I want to bind the data from my XML file to my userControl.

So far I have:

XML file:

<?xml version="1.0" encoding="utf-8"?>
<testData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    <pumps>
        <pump>
            <speed value="1200"/>
            <color value="Black"/>
            <heightAndWidth size="50"/>
        </pump>
    </pumps>
</testData>

App.xaml

<Application.Resources>
    <XmlDataProvider x:Key="testDataDataSource" Source="Desktop\testData.xml" d:IsDataSource="True"/>
</Application.Resources>

UserControl:

<Grid x:Name="LayoutRoot">
    <Rectangle x:Name="rect" Fill="{Binding XPath=color}" />
    <TextBlock x:Name="line1" Text="{Binding XPath=speed}" />
</Grid>

And MainWindow.xaml

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource testDataDataSource}}">
    <local:RectangleControl  DataContext="{Binding Mode=Default, XPath=/testData/pumps/pump}" />
</Grid>

However binding is not working. Could you point out what I am doing wrong.

A: 

Your XPath isn't returning the attribute that contains the value you're trying to bind to. Try this instead:

<Grid x:Name="LayoutRoot">
    <Rectangle x:Name="rect" Fill="{Binding XPath=color/@value}" />
    <TextBlock x:Name="line1" Text="{Binding XPath=speed/@value}" />
</Grid>
Robert Rossney
Right! I forgot that it's an attribute! Thanks.
Vitalij