2010-02-10 67 views
2

我想在xaml.cs的Button_Click事件中將ListBox綁定到Xml。 我的XML是簡單在Button_Click事件中將WPF列表框綁定到Xml?

 <books> 
     <book>ABC/book> 
     <book>XYZ</book> 
    </books> 

下面是我在做什麼..

在xaml.cs

 XmlDocument x = new XmlDocument(); 
     x.LoadXml(e.Result.ToString()); 
     listbox1.ItemsSource = x; 

和XAML

<ListBox x:Name="lstbxTrends" Margin="95,112,8,18" ItemsSource="{Binding XPath=Books}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding [email protected]}" /> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

但是沒有在Listbox中顯示..?

謝謝你們!

回答

2

而是在代碼中設置的ItemsSource的,你需要設置的DataContext:

listbox1.DataContext = x; 

DataContext的是什麼的ItemsSource綁定試圖解決對(既然你沒有指定其他來源的RelativeSource或的ElementName在綁定中)。