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

編集:LINQクエリにToList呼び出しを追加しました

このためのコードビハインドでLINQを使用して、リストボックスの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 に答える