ARZ
ARZ

Reputation: 2491

Using XPath with Path

I have a TreeView that binds to XML data and one DataGrid that binds to SelectedItem of that TreeView using this XAML code:

<DataGrid Name="Dg1">
    <DataGrid.ItemsSource>
        <Binding ElementName="treeView1" Path="SelectedItem.Elements[Book]" />
    </DataGrid.ItemsSource>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Id" Binding="{Binding Path=Attribute[id].Value}"/>
        <DataGridTextColumn Header="Name" Binding="{Binding Path=Attribute[name].Value}"/>
    </DataGrid.Columns>
 </DataGrid>

and this XML:

<Books>
   <Book id="123" name="Big Cat" type="t1" />
   <Book id="124" name="First Man" type="t1" />
   <Book id="125" name="Number One" type="t2" />
</Books>

This works fine but I want to filter the set of Books by some condition using XPath after Path but this doesn't work:

XPath="Book[@type='t1']"

What is the best solution for this problem?

Or is it possible to use 'SelectedEtem' in XPath instead of using Path?!

Upvotes: 0

Views: 4837

Answers (1)

Shoaib Shaikh
Shoaib Shaikh

Reputation: 4585

Use data context to get selected item then in itemssource you can narrow the results by type.

<DataGrid DataContext="{Binding ElementName=treeView1, Path=SelectedItem.Elements[Book]}" ItemsSource="{Binding XPath=Book[@type\=\'t1\'] }" AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="200" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Id" Binding="{Binding Path=Attribute[Id].Value}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Attribute[name].Value}"/>
            </DataGrid.Columns>

        </DataGrid>

EDIT:

 <Grid>
        <Grid.Resources>
            <XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books">
                <x:XData>
                    <Inventory xmlns="">
                        <Books>
                            <Book id="123" name="Big Cat" type="t1" />
                            <Book id="124" name="First Man" type="t1" />
                            <Book id="125" name="Number One" type="t2" />
                        </Books>
                    </Inventory>
                </x:XData>
            </XmlDataProvider>
        </Grid.Resources>
        <Button DataContext="{StaticResource InventoryData}" Tag="{Binding}" Content="Button" Height="23" HorizontalAlignment="Left" Margin="409,134,0,0" Name="button1" VerticalAlignment="Top" Width="75" />


        <DataGrid DataContext="{Binding ElementName=button1, Path=Tag}" ItemsSource="{Binding XPath=Book[@type\=\'t1\'] }" AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="200" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Id" Binding="{Binding Path=Attributes[id].Value}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Attributes[name].Value}"/>
            </DataGrid.Columns>

        </DataGrid>

    </Grid>

Regards

Upvotes: 1

Related Questions