views:

3736

answers:

2

The following example successfully binds objects with a list box to display them. However, I would like to create all the objects in one class and then from another class query them with LINQ to fill my XAML Listbox, what would I need to add this example:

XAML:

<Window x:Class="WpfApplication15.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"
        xmlns:local="clr-namespace:WpfApplication15">
        <Window.Resources>
            <ObjectDataProvider x:Key="customers" ObjectType="{x:Type local:Customers}"/>
            <DataTemplate x:Key="LastNameFirst" DataType="WpfApplication15.Customer">
                <StackPanel Margin="10 10 10 0" Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=LastName}" FontWeight="bold"/>
                    <TextBlock Text=", "/>
                    <TextBlock Text="{Binding Path=FirstName}"/>
                </StackPanel>
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <ListBox
                ItemsSource="{Binding Source={StaticResource customers}}"
                ItemTemplate="{StaticResource LastNameFirst}"/>
        </Grid>
    </Window>

Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication15
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Customer(string firstName, string lastName)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
        }
    }

    public class Customers : List<Customer>
    {
        public Customers()
        {
            this.Add(new Customer("Jim", "Thompson"));
            this.Add(new Customer("Julie", "Watson"));
            this.Add(new Customer("John", "Walton"));
        }
    }

}
+1  A: 

Take a look @ http://www.codeplex.com/bindablelinq and you should find a bunch of good examples for this.

Steve Godbold
+3  A: 

edit: added ToList call to the LINQ query

You can just assign the ItemsSource of the ListBox using LINQ in code-behind for this. Assuming you give your ListBox a name:

<Window x:Class="WpfApplication15.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:local="clr-namespace:WpfApplication15"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="300" Height="300" Title="Window1">
    <Window.Resources>
        <DataTemplate x:Key="LastNameFirst" DataType="WpfApplication15.Customer">
            <StackPanel Margin="10 10 10 0" Orientation="Horizontal">
                <TextBlock FontWeight="bold" Text="{Binding Path=LastName}"/>
                <TextBlock Text=", "/>
                <TextBlock Text="{Binding Path=FirstName}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListBox x:Name="lstCustomers"
                 ItemTemplate="{StaticResource LastNameFirst}"/>
    </Grid>
</Window>

You can assign to ItemsSource in the Loaded event:

public partial class Window1 : Window 
{
    public Window1()
    {
        this.Loaded += new RoutedEventHandler(Window1_Loaded);
        InitializeComponent(); 
    }

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        Customers customers = new Customers();
        lstCustomers.ItemsSource = customers.Where(customer => customer.LastName.StartsWith("W")).ToList();
    }
}

Assuming your LINQ query will change depending on some logic, you can re-assign ItemsSource at the appropriate points.

If you want to bind without dipping into code-behind whenever your query logic changes, you're probably better off using a CollectionViewSource, since it has sorting and filtering capabilities (assuming that's what you're after from using LINQ).

Robert Macnee
I tried binding my data with the LINQ example you provided, but it causes the rows in my DataGrid to be empty, I posted a question about that here: http://stackoverflow.com/questions/503014/wpf-datagrid-is-filled-except-when-i-use-linq-to-filter-its-items
Edward Tanguay
I've updated my answer with the ToList call from that thread. It seems that ItemsControls don't correctly query LINQ IEnumerables assigned to their ItemsSource property.
Robert Macnee