绑定ListView到ObservableCollection和Linq

本文关键字:Linq ObservableCollection ListView 绑定 | 更新日期: 2023-09-27 18:06:50

嗨,我正在尝试绑定一个ListView到Linq,并使它对集合的变化做出反应。

public partial class MainWindow
{
    readonly ObservableCollection<Person> _persons = new ObservableCollection<Person>();
    readonly Team _team1 = new Team { Name = "Team 1" };
    readonly Team _team2 = new Team { Name = "Team 2" };
    readonly Team _team3 = new Team { Name = "Team 3" };
    public MainWindow()
    {
        InitializeComponent();
        _persons.Add(new Person {Name = "James", Team = _team1});
        _persons.Add(new Person {Name = "John", Team = _team2});
        _persons.Add(new Person {Name = "Peter", Team = _team3});
        _persons.Add(new Person {Name = "Jack", Team = _team1});
        _persons.Add(new Person {Name = "Jones", Team = _team2});
        _persons.Add(new Person {Name = "Bauer", Team = _team3});
        _persons.Add(new Person {Name = "Bo", Team = _team1});
        _persons.Add(new Person {Name = "Ben", Team = _team2});
        _persons.Add(new Person {Name = "Henry", Team = _team3});
        TeamList1.ItemsSource = from person in _persons where person.Team == _team1 select person;
        TeamList2.ItemsSource = from person in _persons where person.Team == _team2 select person;
        TeamList3.ItemsSource = from person in _persons where person.Team == _team3 select person;
    }
    private void ButtonClick(object sender, RoutedEventArgs e)
    {
        _persons[0].Team = _team2;
    }
}

And my Models

 public class Person : INotifyPropertyChanged
    {
        private string _name;
        public String Name
        {
            get { return _name; }
            set { _name = value; NotifyPropertyChanged("Name"); }
        }
        private Team _team;
        public Team Team
        {
            get { return _team; }
            set { _team = value; NotifyPropertyChanged("Team"); }
        }
        public override string ToString()
        {
            return string.Format("Name {0}'t{1}", Name, Team);
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
    public class Team : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; NotifyPropertyChanged("Name"); }
        }
        public override string ToString()
        {
            return Name;
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }

和Xaml

<Grid>
        <StackPanel Orientation="Vertical">
        <StackPanel>
            <ListView Name="TeamList1"/>
            <ListView Name="TeamList2"/>
            <ListView Name="TeamList3"/>
        </StackPanel>
            <Button Content="Click Me" Click="ButtonClick"/>
        </StackPanel>
    </Grid>

当ButtonClick被调用时,我希望我的ListView反映它对_persons所做的更改。

绑定ListView到ObservableCollection和Linq

只是不要使用linq,有CollectionViews这类东西有过滤器,然后你只需要刷新视图(参见:如何:在视图中过滤数据)。

(如果你必须使用linq,它也有可绑定的扩展名)

LINQ查询在绑定到数据源并生成列表视图中的项时进行计算。当您更改原始列表_persons中的一个人的属性时,LINQ查询将而不是被重新计算。要实现这一点,您必须做一个自定义实现,侦听原始列表中每个PersonPropertyChanged事件,并相应地更新ObservableCollection<Person>。你要绑定到列表视图的可观察对象集合

你需要我的observablecalcultions库。使用这个库,您可以编写如下代码:

    TeamList1.ItemsSource = _persons.Filtering(person.Team == _team1);
    TeamList2.ItemsSource = _persons.Filtering(person.Team == _team2);
    TeamList3.ItemsSource = _persons.Filtering(person.Team == _team3);