2010-01-12 8 views
1

두 번째 Listbox에 대한 XmlDataprovider 소스를 제공하려면 Listbox1의 선택된 항목이 필요합니다.WPF 상위/하위 목록 상자를 설정하는 방법

에 ListBox1 용도 :

<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" 
       /> 
,691

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

예 :

<?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을 Category.xml

내가 겪고있는 문제는 XmlFileName을 itemXML의 소스로 만들기위한 올바른 구문을 찾는 것입니다. 사용자는 ListBox1에서 <name>을 선택하고 Listbox2를 피드하는 itemXML에 <XmlFileName>을 전송합니다.

+1

팁 : SO는 HTML 태그로 취급하므로 XML (및 XAML) 태그는 일반 텍스트로 표시되지 않으며 브라우저는이를 인식하지 못하기 때문에 자동으로 렌더링하지 않습니다. 백틱이나 코드 (101010) 버튼을 사용하여 나타나게하십시오. – itowlson

답변

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 속성을 설정할 수도 있습니다. 그것도 새로 고칩니다.

관련 문제