views:

2107

answers:

1

Hi,

I've been having a problem binding a ListView to an Object using LINQ. It's best explained with a testcase I've created:

C#:

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<MyClass> mylist = new ObservableCollection<MyClass>();
            mylist.Add(cls);
            mylist.Add(cls);

            listView1.ItemsSource = (from c in mylist select new List<MyClass> {c}).ToList();

            label1.Content = listView1.Items.Count.ToString();
        }
    }
}

XAML:

<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">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <ListView ItemsSource="{Binding}" Name="listView1" Height="200" Grid.Row="0">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Title" Width="80" DisplayMemberBinding="{Binding Path=title}" />
                    <GridViewColumn Header="Subtitle" Width="80" DisplayMemberBinding="{Binding subclass.subtitle}" />
                </GridView>
            </ListView.View>
        </ListView>
        <Label Name="label1" Grid.Row="1" ></Label>
    </Grid>
</Window>

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?

Thanks, Ian

+4  A: 

(updated)

Re the xaml, it look like you're missing a "Path=" in the line:

{Binding subclass.subtitle}

Re the objects; it is possible that it wants properties; try:

    public class MySubClass {
        public string subtitle {get;set;}
    }
    public class MyClass
    {
        public string title {get;set;}
        public MySubClass subclass {get;set;}
    }

The line:

(from c in mylist select new List<MyClass> {c}).ToList();

creates a list of lists, each with one element (and two items in the outer list). Just set mylist as the source:

listView1.ItemsSource = mylist;

If you want to do an "interesting" projection inside the LINQ query, then it would look something more like:

(from c in mylist select new { Foo = c.SomeProp,
    Bar = c.SomeOtherProp + 12 }).ToList();

This is then a single list, just with different items to the original list.

Marc Gravell
Hi MarcHave attempted that - it doesn't make any difference sadly. In addition, the LINQ query is more complex in the real app so I'd be interested to see a solution which retains the LINQ query.Thanks!
Ian Gregory
Thanks Marc. I've fixed the XAML typo, and gone back to the simple case using listView1.ItemsSource = mylist; but the listview items still don't show on the form - yet two empty listview rows are clickable. The objects have public fields; the class definitions are at the beginning of the C# above.
Ian Gregory
See update; try using properties (I posted the code to do so)
Marc Gravell
Marc - it does indeed want properties. This also works in my full app. Many thanks!
Ian Gregory