如何根据价值绑定数据?

时间:2013-03-29 19:27:45

标签: xaml microsoft-metro

我有ObservableCollection和值,需要在集合中找到该项目。有任何想法吗? (p.s converter并不是个好主意,因为我有很多收藏品)

1 个答案:

答案 0 :(得分: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"); } }
    }
}