需要使用LINQ将对象绑定到Listbox的简单示例

Edw*_*uay 3 linq data-binding wpf xaml listbox

以下示例使用a成功绑定对象ListBox以显示它们.但是,我想在一个类中创建所有对象,然后从另一个类使用LINQ查询它们以填充我的XAML ListBox,我需要添加这个示例:

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>
Run Code Online (Sandbox Code Playgroud)

代码背后:

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"));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*nee 5

编辑:添加ToList调用LINQ查询

您可以在代码隐藏中使用LINQ为此分配ListBox的ItemsSource.假设您为ListBox命名:

<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>
Run Code Online (Sandbox Code Playgroud)

您可以在Loaded事件中分配ItemsSource:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

假设您的LINQ查询将根据某些逻辑而更改,您可以在适当的位置重新分配ItemsSource.

如果你想在你的查询逻辑发生变化时不进行代码隐藏而进行绑定,那么最好使用CollectionViewSource,因为它具有排序过滤功能(假设你使用的是LINQ).