将ListView绑定到ObservableCollection和Linq

时间:2011-08-29 10:00:56

标签: c# wpf linq xaml data-binding

您好我试图将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;
    }
}

我的模特

 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上所做的更改。

3 个答案:

答案 0 :(得分:3)

只是不要使用linq,有CollectionViews这类有过滤器的东西,你只需要刷新视图(参见:How to: Filter data in a view)。 (如果你必须使用linq,它还有bindable extensions

答案 1 :(得分:1)

当LINQ查询绑定到数据源并生成列表视图中的项目时,将对其进行评估。每当您更改原始列表_persons内的人员属性时,LINQ查询将重新评估。为此,您必须执行一个自定义实现,以侦听原始列表中每个PropertyChanged的{​​{1}}事件并相应地更新Person。这个可观察的集合,你将绑定到列表视图。

答案 2 :(得分:0)

您需要我的ObservableComputations库。使用该库,您可以像这样进行编码:

    II ii = <any instance of classes that implements interface II>
    Number n = ii.get();