3

以下示例成功地将对象与 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>

后面的代码:

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"));
        }
    }
}
4

2 回答 2

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>

您可以在 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();
    }
}

假设您的 LINQ 查询将根据某些逻辑而改变,您可以在适当的点重新分配 ItemsSource。

如果您想在查询逻辑发生更改时绑定而不深入到代码隐藏中,则最好使用CollectionViewSource,因为它具有排序过滤功能(假设这是您使用 LINQ 所追求的)。

于 2009-01-30T17:54:57.353 回答
1

看看@ http://www.codeplex.com/bindablelinq,你应该找到一堆很好的例子。

于 2009-01-30T12:30:35.713 回答