2013-03-29 43 views
0

我有ObservableCollection和值需要找到集合中的項目。有任何想法嗎? (p.s轉換器不是好主意,因爲我有很多集合)如何根據值綁定數據?

+0

我有一個很難理解你的意思。請提供更多細節以及您嘗試的內容。 –

+0

'DataContext =「{Binding Path = source_id,Converter = {StaticResource idToFUser},ConverterParameter = {StaticResource Profiles}}」' 此代碼在轉換器中給我ObservableCollection <>配置文件 –

回答

0

此功能(應用濾鏡)屬於ViewModel。這裏有一個簡單的例子來說明。

您可能還想看看CollectionViewSource以獲得同一概念的更精緻版本。

enter image description here

的XAML:

<Window x:Class="WpfApplication1.MainWindow" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:local="clr-namespace:WpfApplication1" 
       Title="MainWindow" Height="350" Width="525"> 

    <Window.DataContext> 
     <local:ViewModel /> 
    </Window.DataContext> 

    <StackPanel Orientation="Horizontal" VerticalAlignment="Top" > 
     <ListBox ItemsSource="{Binding MyClasses}" DisplayMemberPath="Name" Margin="5" /> 
     <ListBox ItemsSource="{Binding MyFilteredClasses}" DisplayMemberPath="Name" Margin="5" /> 
     <TextBox Text="{Binding MySelectedClass.Name}" Margin="5" /> 
    </StackPanel> 
</Window> 

視圖模型:

using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Linq; 

namespace WpfApplication1 
{ 
    public class ViewModel : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     private void OnPropertyChanged(string propertyName) 
     { 
      if (this.PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     private ObservableCollection<Class1> _myClasses; 
     public ObservableCollection<Class1> MyClasses { get { return _myClasses; } set { _myClasses = value; OnPropertyChanged("MyClasses"); } } 

     private List<Class1> _myFilteredClasses; 
     public List<Class1> MyFilteredClasses { get { return _myFilteredClasses; } set { _myFilteredClasses = value; OnPropertyChanged("MyFilteredClasses"); } } 

     private Class1 _mySelectedClass; 
     public Class1 MySelectedClass { get { return _mySelectedClass; } set { _mySelectedClass = value; OnPropertyChanged("MySelectedClass"); } } 


     public ViewModel() 
     { 
      MyClasses = new ObservableCollection<Class1>() 
      { 
       new Class1() { Name = "Connelly" }, 
       new Class1() { Name = "Donnelly" }, 
       new Class1() { Name = "Fonnelly" }, 
       new Class1() { Name = "McGregor" }, 
       new Class1() { Name = "Griffiths" } 
      }; 

      // filter your ObservableCollection by some criteria, and bind to the result (either another list, or just one item) 
      MyFilteredClasses = MyClasses.Where(c => c.Name.EndsWith("onnelly")).ToList(); 
      MySelectedClass = MyClasses.FirstOrDefault(c => c.Name.StartsWith("Mc")); 
     } 
    } 

    public class Class1 : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged(string propertyName) 
     { 
      if (this.PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     private string _name; 
     public string Name { get { return _name; } set { _name = value; OnPropertyChanged("Name"); } } 
    } 
}