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

需要使用LINQ将WPF对象绑定到Listbox的简单示例吗,wpf,linq,xaml,data-binding,listbox,Wpf,Linq,Xaml,Data Binding,Listbox,以下示例使用列表框成功绑定对象以显示它们。 但是,我想在一个类中创建所有对象,然后从另一个类中使用LINQ查询它们,以填充我的XAMLListBox,我需要添加什么来添加此示例: XAML: 代码隐藏: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using S

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

XAML:


代码隐藏:

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"));
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication15
{
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
}
}
公共类客户
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共客户(字符串firstName、字符串lastName)
{
this.FirstName=FirstName;
this.LastName=LastName;
}
}
公共类客户:列表
{
公众客户()
{
添加(新客户(“吉姆”、“汤普森”);
添加(新客户(“朱莉”、“沃森”);
添加(新客户(“约翰”、“沃尔顿”);
}
}
}
看一看@,您应该会找到一堆很好的例子。

编辑:添加了对LINQ查询的ToList调用

为此,您可以使用LINQ in代码隐藏来分配ListBox的ItemsSource。假设您为列表框命名:

<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>
假设您的LINQ查询将根据某些逻辑而改变,您可以在适当的点重新分配ItemsSource


如果您希望在查询逻辑更改时绑定而不使用代码隐藏,那么最好使用,因为它具有和功能(假设您使用LINQ就是为了实现这一点)。

我尝试使用您提供的LINQ示例绑定数据,但这会导致数据网格中的行为空,我在这里发布了一个问题:我已经用那个帖子的ToList调用更新了我的答案。ItemsControl似乎无法正确查询分配给其ItemsSource属性的LINQ IEnumerables。
<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>
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();
    }
}