tags:

views:

300

answers:

2

I need selected item of Listbox1 to provide XmlDataprovider source for second Listbox.

Listbox1 uses:

<XmlDataProvider x:Key="CategoryXML"
                 Source="C:\Category.xml"
                 XPath="category"
                 /> 

Ex: Category.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<main>
<category>
    <name>Local</name>
    <XmlFileName>C:\Doc1.xml</XmlFileName>
</category>
<category>
    <name>National</name>
    <XmlFileName>C:\Doc2.xml</XmlFileName>
</category>
<category>
    <name>Global</name>
    <XmlFileName>C:\Doc3.xml</XmlFileName>
</category>
</main>

xaml:

<ListBox ItemsSource="{Binding Source={StaticResource CategoryXML},XPath=//main//category}"
         SynchronizedWithCurrentItem="True" Name="CategoryList">

ListBox2:

<XmlDataProvider x:Key="itemXML"
                 Source="?" **XmlFileName of select item in Listbox1**
                 XPath="item"
                 /> 

The problem I'm having is finding the correct syntax for making XmlFileName the source for itemXML. The user will select the <name> in ListBox1 and send the <XmlFileName> to itemXML which feeds Listbox2

A: 

I don't know that you can do it through ordinary data binding. But it's easy with a little code-behind:

<Window x:Class="WpfApplication11.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <XmlDataProvider x:Key="Master" Source="Master.xml" XPath="Master/Item"/>
        <XmlDataProvider x:Key="Detail" XPath="Detail/Item"/>
    </Window.Resources>
    <StackPanel>
        <ListBox 
            Name="MasterList"
            ItemsSource="{Binding Source={StaticResource Master}}"
            SelectionChanged="MasterList_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding XPath=Description}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <ListBox Name="DetailList" ItemsSource="{Binding Source={StaticResource Detail}}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding XPath=Description, Mode=TwoWay}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Window>

And then:

private void MasterList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListBox lb = sender as ListBox;
    XmlElement elm = lb.SelectedItem as XmlElement;
    string filename = elm.SelectSingleNode("Filename").InnerText;
    XmlDocument d = new XmlDocument();
    d.Load(filename);
    XmlDataProvider p = Resources["Detail"] as XmlDataProvider;
    p.Document = d;
}

This works because an XmlDataProvider refreshes whenever you set its Document property. You could also just construct a URI from the filename and set the Source property; that refreshes it too.

Robert Rossney
A: 

Thank You Robert, your idea/code worked perfectly. I learned something new for which I am very grateful. Thanks again

Rick
You must add comment to the answer, not create a new answer
Ricibald