Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to debug binding an XDocument to a WPF ListView
    primarykey
    data
    text
    <p>I think I'm missing something fundamental in WPF data binding here:</p> <p>I have this XML file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;WindowList&gt; &lt;Window Height="10" Width="10" ALL_MODEL_MODEL="xyz0" ALL_MODEL_MANUFACTURER="leidi"/&gt; &lt;Window Height="20" Width="20" ALL_MODEL_MODEL="xyz1" ALL_MODEL_MANUFACTURER="goffin"/&gt; &lt;Window Height="30" Width="30" ALL_MODEL_MODEL="xyz2" ALL_MODEL_MANUFACTURER="schlueter"/&gt; &lt;Window Height="40" Width="40" ALL_MODEL_MODEL="xyz3" ALL_MODEL_MANUFACTURER="rossi"/&gt; &lt;/WindowList&gt; </code></pre> <p>And this XAML file:</p> <pre><code>&lt;Window x:Class="BindListToXDocument.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;StackPanel&gt; &lt;ListView Name="lstWindows"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="Height" DisplayMemberBinding="{Binding Path=Attribute[Height].Value}"/&gt; &lt;GridViewColumn Header="Width" DisplayMemberBinding="{Binding Path=Attribute[Width].Value}"/&gt; &lt;GridViewColumn Header="Model" DisplayMemberBinding="{Binding Path=Attribute[Model].Value}"/&gt; &lt;GridViewColumn Header="Manufacturer" DisplayMemberBinding="{Binding Path=Attribute[Manufacturer].Value}"/&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;Button Click="Button_Click"&gt;Populate List&lt;/Button&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>The binding expressions are modeled after this Document on msdn: <a href="http://msdn.microsoft.com/en-us/library/cc165615.aspx" rel="nofollow">How to: Bind to XDocument, XElement, or LINQ for XML Query Results</a>.</p> <p>I then use the following code to set the <code>DataContext</code>:</p> <pre><code>private void Button_Click(object sender, RoutedEventArgs e) { var doc = XDocument.Load("WindowList.xml"); var windows = doc.Root.Elements(); lstWindows.DataContext = windows; } </code></pre> <p>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 <code>XDocument</code> specifically?</p> <p><strong>EDIT:</strong> OK, it seems I was missing something crucial in my XAML:</p> <pre><code>&lt;ListView Name="lstWindows" ItemsSource="{Binding}"&gt; &lt;!-- specify the ItemsSource! --&gt; </code></pre> <p>I'll leave this question here, since it is a minimal example of binding to an <code>XDocument</code>, something I was having trouble finding - most examples already went a step further. I'm still interested in tips for debugging, though.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload