2012-01-31 35 views
2

我有這些節點的XML文件:如何使用WPF XAML中的master-detail綁定XML數據?

<Product> 
    <Name>... 
    <Color>... 
    <Price>... 
</Product> 

我有一個顯示所有的名稱在這樣的XML文件列表框:

<ListBox Name="listBox1" ItemsSource="{Binding}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <DockPanel > 
        <TextBlock Text = "{Binding Name}" /> 
       </DockPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

當在列表框中的項目被選中,我想在2個標籤控件上顯示所選產品的顏色和價格。

如何將顏色和價格綁定到選定的名稱? 這意味着我需要從XML文件中獲取顏色和價格信息,因爲列表框只有名稱。 謝謝。

+0

謝謝 - 我發現這個問題,下面的答案非常有幫助。 – 2013-09-05 13:55:34

回答

3

SelectedItem將是整個項目,所有的三種元素,所以這樣的事情應該做的:

<StackPanel DataContext="{Binding SelectedItem, ElementName=listBox1}"> 
    <TextBlock Text="{Binding XPath=Color}" /> 
    <TextBlock Text="{Binding XPath=Price}" /> 
</StackPanel> 

(能使用Binding.StringFormat前面加上一個標籤,也Label控制本身是標籤的東西,不是顯示文字)

+0

謝謝 - 環顧四周,這正是我所需要的。 – 2013-09-05 13:55:12