2013-03-29 4 views
0

ObservableCollection 및 컬렉션의 항목을 찾아야하는 값이 있습니다. 어떤 아이디어? (많은 컬렉션을 가지고 있기 때문에 PSD 컨버터는 좋지 않습니다.)값에 따라 데이터를 바인딩하는 방법은 무엇입니까?

+0

내가 힘든 시간을 당신이 무슨 뜻인지 이해하는 데 문제가 있습니다. 더 많은 정보를 제공해주십시오. –

+0

'DataContext = "{Binding Path = source_id, 변환기 = {StaticResource idToFUser}, ConverterParameter = {StaticResource 프로파일}}"' 변환기의이 코드는 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"); } } 
    } 
} 
관련 문제