2009-01-30 54 views
3

以下示例成功綁定具有ListBox的對象以顯示它們。 不過,我想在一個類中創建的所有對象,然後從另一個類查詢他們LINQ來填補我的XAML ListBox,我會需要添加這個例子:需要使用LINQ的WPF綁定對象到列表框的簡單示例

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

回答

5

編輯:添加ToList調用LINQ查詢

你可以只在指定的代碼隱藏使用LINQ這個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> 

可以分配到的ItemsSource在Loaded事件:

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,因爲它有sortingfiltering能力(假設這是你使用LINQ後在做什麼)綁定。

+0

我試圖綁定我的數據與您提供的LINQ示例,但它導致我的DataGrid中的行爲空,我在這裏發佈了一個有關該問題:http://stackoverflow.com/questions/503014/wpf-datagrid -is-filled-except-when-i-use-linq-to-filter-its-items – 2009-02-02 12:24:13