我需要Listbox1的选定项目来为第二个Listbox提供XmlDataprovider源。
Listbox1使用:
<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"
/>
我遇到的问题是找到使XmlFileName成为itemXML源的正确语法。用户将在ListBox1中选择<name>
并将<XmlFileName>
发送到为Feedbox2提供的itemXML
答案 0 :(得分:0)
我不知道你可以通过普通的数据绑定来做到这一点。但是有一点代码隐藏很容易:
<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>
然后:
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;
}
这是有效的,因为每当您设置XmlDataProvider
属性时Document
都会刷新。您也可以从文件名构造一个URI并设置Source
属性;也刷新它。