2012-02-02 86 views
2

我有一些數據保存在xml文件中。 這些將顯示在列表框中。 現在,當我更改列表框Selectedindex時,我想根據selectedindex更新textblock中的其他信息。將textblock綁定到純xaml中的當前列表框項目

有沒有辦法在純xaml中做到這一點? 如果不是,我將如何將Textblock綁定到列表框中的selecteditem?

編輯: 我如何瀏覽數據而不使用列表框?我的意思是使用一個按鈕移動到下一個項目和其他按鈕向後移動..!

任何幫助非常感謝..

<Window x:Class="WpfSingleInstance.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"> 
<Grid> 
    <StackPanel 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Background="Cornsilk"> 

     <StackPanel.Resources> 
      <XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books"> 
       <x:XData> 
        <Inventory xmlns=""> 
         <Books> 
          <Book ISBN="0-7356-0562-9" Stock="in" Number="9"> 
           <Title>XML in Action</Title> 
           <Summary>XML Web Technology</Summary> 
          </Book> 
          <Book ISBN="0-7356-1370-2" Stock="in" Number="8"> 
           <Title>Programming Microsoft Windows With C#</Title> 
           <Summary>C# Programming using the .NET Framework</Summary> 
          </Book> 
          <Book ISBN="0-7356-1288-9" Stock="out" Number="7"> 
           <Title>Inside C#</Title> 
           <Summary>C# Language Programming</Summary> 
          </Book> 
         </Books> 
        </Inventory> 
       </x:XData> 
      </XmlDataProvider> 
     </StackPanel.Resources> 

     <TextBlock FontSize="18" FontWeight="Bold" Margin="10" 
HorizontalAlignment="Center">XML Data Source Sample</TextBlock> 
     <ListBox 
Width="265" Height="98" x:Name="lbox" Background="Honeydew" IsSynchronizedWithCurrentItem="True"> 
      <ListBox.ItemsSource> 
       <Binding Source="{StaticResource InventoryData}" 
      XPath="*[@Stock='out'] | *[@Number>=8 or @Number=3]"/> 
      </ListBox.ItemsSource> 

      <ListBox.ItemTemplate> 

       <DataTemplate> 
        <TextBlock FontSize="12" Foreground="Red"> 
     <TextBlock.Text> 
     <Binding XPath="Title"/> 
     </TextBlock.Text> 
        </TextBlock> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

     <StackPanel DataContext="{StaticResource InventoryData}"> 
      <TextBlock Text="{Binding XPath=Book/Title}"/> 
      <TextBox Margin="5,31,98,10" x:Name="textBoxMainDetail" Text="{Binding XPath=Book/Summary}" /> 
     </StackPanel> 
    </StackPanel> 



</Grid> 

回答

2

您可以結合這樣的:

<TextBox Text="{Binding ElementName=lbox, Path=SelectedItem[Title].InnerText}" /> 

的SelectedItem是XmlElement

編輯:下面是一些示例代碼,如何訪問後面的代碼中的XmlDataProvider的數據,並將其應用爲TextBox的DataContent。

更改TextBox.Text像這樣綁定:

<TextBox x:Name="textBoxMainDetail" Text="{Binding Path=[Title].InnerText}" /> 

在代碼中從背後XmlDataProvider獲取XML數據和設置TextBox的DataContext的:

XmlDataProvider dataProvider = (XmlDataProvider)stackPanel.Resources["InventoryData"]; 
XmlElement books = (XmlElement)dataProvider.Document.SelectNodes(dataProvider.XPath)[0]; 

// set DataContext to an item from the child node collection 
textBoxMainDetail.DataContext = books.ChildNodes[0]; 

注意,StackPanel中有其資源字典中的XmlDataProvider現在有一個名稱。如果此代碼應在應用程序初始化期間運行(例如,在Window構造函數中),則必須將XmlDataProvider.IsAsynchronous屬性設置爲false。

您現在應該可以將DataContext更改爲按鈕單擊處理程序中書籍集合的另一個索引項目。

+0

您好,感謝的作品......可我還添加了一個按鈕,通過數據導航,而不使用列表框?我的意思是,如果我刪除列表框,我將如何瀏覽XML數據?moveforward(),moveback()等.. – lebhero 2012-02-02 12:43:32

+0

我猜在沒有列表框的情況下瀏覽數據可能會更好地在代碼後面完成。有些東西應該跟蹤當前/選定的項目。 – Clemens 2012-02-02 13:11:20

+0

任何代碼或例子?? Beispiele :) – lebhero 2012-02-02 13:14:34

2

您需要將SelectedValuePath設置爲列表框的「標題」。然後,只需將您的文本塊綁定到你的列表框的使用的ElementName這樣的了selectedValue -

<ListBox Width="265" Height="98" x:Name="lbox" Background="Honeydew" 
        IsSynchronizedWithCurrentItem="True" SelectedValuePath="Title"> 
      </ListBox> 
      <StackPanel DataContext="{StaticResource InventoryData}"> 
       <TextBlock Text="{Binding Path=SelectedValue, ElementName=lbox}"/> 
       <TextBox Margin="5,31,98,10" x:Name="textBoxMainDetail" Text="{Binding XPath=Book/Summary}" /> 
      </StackPanel> 
     </StackPanel> 
相關問題