views:

78

answers:

1

Hi,

I am trying to bind a ListBox to Xml in Button_Click event in xaml.cs. my Xml is simple

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

Here's what I am doing..

in xaml.cs

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

and Xaml is

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

But nothing shows up in Listbox..?

Thanks guys!

+1  A: 

Instead of setting the ItemsSource in code, you need to set the DataContext:

listbox1.DataContext = x;

The DataContext is what the ItemsSource binding is trying to resolve against (since you have specified no other Source, RelativeSource or ElementName in the Binding).

itowlson