0

我有需要在集合中找到项目的 ObservableCollection 和值。有任何想法吗?(ps转换器不是好主意,因为我有很多收藏品)

4

1 回答 1

0

此功能(应用过滤器)属于 ViewModel。这是一个简单的示例。

您可能还想查看CollectionViewSource以获得相同概念的更精致版本。

在此处输入图像描述

xml:

<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"); } }
    }
}
于 2013-03-30T10:51:18.060 回答