Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ to Objects - binding to a ListView
    primarykey
    data
    text
    <p>I've been having a problem binding a ListView to an Object using LINQ. It's best explained with a testcase I've created:</p> <p>C#:</p> <pre><code>using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using System.Windows.Controls; namespace WpfApplication1 { public partial class Window1 : Window { public Window1() { InitializeComponent(); } public class MySubClass { public string subtitle; } public class MyClass { public string title; public MySubClass subclass; } private void Window_Loaded(object sender, RoutedEventArgs e) { MySubClass sub = new MySubClass(); sub.subtitle = "This is the subtitle"; MyClass cls = new MyClass(); cls.title = "This is the title"; cls.subclass = sub; ObservableCollection&lt;MyClass&gt; mylist = new ObservableCollection&lt;MyClass&gt;(); mylist.Add(cls); mylist.Add(cls); listView1.ItemsSource = (from c in mylist select new List&lt;MyClass&gt; {c}).ToList(); label1.Content = listView1.Items.Count.ToString(); } } } </code></pre> <p>XAML:</p> <pre><code>&lt;Window x:Class="WpfApplication1.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" Loaded="Window_Loaded"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;ListView ItemsSource="{Binding}" Name="listView1" Height="200" Grid.Row="0"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="Title" Width="80" DisplayMemberBinding="{Binding Path=title}" /&gt; &lt;GridViewColumn Header="Subtitle" Width="80" DisplayMemberBinding="{Binding subclass.subtitle}" /&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;Label Name="label1" Grid.Row="1" &gt;&lt;/Label&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>When run, I'd expect this code to show the title and subtitle properties in the listview. It doesn't, but the listview Count() is correctly shows that it has 2 items. I think i'm binding to the wrong property.... should I be using a different syntax in the binding?</p> <p>Thanks, Ian</p>
    singulars
    1. This table or related slice is empty.
    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