开发者

How to debug binding an XDocument to a WPF ListView

I think I'm missing something fundamental in WPF data binding here:

I have this XML file:

<?xml version="1.0" encoding="utf-8" ?>
<WindowList>
  <Window Height="10" Width="10" ALL_MODEL_MODEL="xyz0" ALL_MODEL_MANUFACTURER="leidi"/>
  <Window Height="20" Width="20" ALL_MODEL_MODEL="xyz1" ALL_MODEL_MANUFACTURER="goffin"/>
  <Window Height="30" Width="30" ALL_MODEL_MODEL="xyz2" ALL_MODEL_MANUFACTURER="schlueter"/>
  <Window Height="40" Width="40" ALL_MODEL_MODEL="xyz3" ALL_MODEL_MANUFACTURER="rossi"/>
</WindowList>

And this XAML file:

<Window x:Class="BindListToXDocument.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pres开发者_StackOverflow中文版entation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <ListView Name="lstWindows">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Height" 
                     DisplayMemberBinding="{Binding Path=Attribute[Height].Value}"/>
                    <GridViewColumn Header="Width" 
                     DisplayMemberBinding="{Binding Path=Attribute[Width].Value}"/>
                    <GridViewColumn Header="Model" 
                     DisplayMemberBinding="{Binding Path=Attribute[Model].Value}"/>
                    <GridViewColumn Header="Manufacturer" 
                     DisplayMemberBinding="{Binding Path=Attribute[Manufacturer].Value}"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Button Click="Button_Click">Populate List</Button>
    </StackPanel>
</Window>

The binding expressions are modeled after this Document on msdn: How to: Bind to XDocument, XElement, or LINQ for XML Query Results.

I then use the following code to set the DataContext:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var doc = XDocument.Load("WindowList.xml");
    var windows = doc.Root.Elements();
    lstWindows.DataContext = windows;
}

The code runs without any complaints, when I click the button, but the list items never show up. I can't figure out how to go about debugging this. Do you have any pointers for debugging binding expressions in general or how to bind the XDocument specifically?

EDIT: OK, it seems I was missing something crucial in my XAML:

<ListView Name="lstWindows" ItemsSource="{Binding}"> <!-- specify the ItemsSource! -->

I'll leave this question here, since it is a minimal example of binding to an XDocument, something I was having trouble finding - most examples already went a step further. I'm still interested in tips for debugging, though.


I did these changes to populate the view...

XAML: set the itemssource property of ListView

<ListView Name="lstWindows" Height="400" ItemsSource="{Binding Path=Elements}">

.CS file : create a property which returns IEnumerable

public IEnumerable<XElement> Elements
{
    get
        {
            var doc = XDocument.Load(@"\abc.xml");
            var windows = doc.Root.Elements();
            return windows;
        }
}

in load method of window... or you can also do this in button click event

this.DataContext = this; or lstWindows.DataContext = this;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜