c# - 将 ListView 绑定(bind)到 ObservableCollection 和 Linq

标签 c# wpf linq xaml data-binding

您好,我正在尝试将 ListView 绑定(bind)到 Linq,并使其对集合中的更改使用react。

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;
    }
}

还有我的模特

 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 反射(reflect)它对 _persons 所做的更改。

最佳答案

别用linq就好了,有CollectionViews对于这种有过滤器的东西,你只需要刷新 View (参见:How to: Filter data in a view)。 (如果你必须使用 linq,也有 bindable extensions)

关于c# - 将 ListView 绑定(bind)到 ObservableCollection 和 Linq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7228656/

相关文章:

linq - GroupBy OrderBy Linq - 无效列

c# - 使用 LINQ 动态选择所有 XML 元素及其值

c# - Xamarin.android 项目,如何删除警告 : duplicate zip entry [classes. jar...]

c# - 如何生成和显示序列(借阅练习)

WPF TextBox.Text 与 MultiBinding

c# - 来自 Mosha Pasumansky 的 XAML 数据透视表

Python 或 WPF 报告应用程序

c# - LINQ - 返回包含基于多个不同列的表的所有列的行

c# - 如何编码为同一原始字符串字符提供不同编码字符的字符串?

c# - 验证站点/应用程序以访问 Web API 服务