2011-08-29 71 views
1

嗨我試圖將一個ListView綁定到Linq,並使其對集合中的更改作出反應。將ListView綁定到ObservableCollection和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所做的更改。

回答

1

當LINQ查詢綁定到數據源並生成列表視圖中的項目時,將對其進行評估。 LINQ查詢將而不是每當您更改原始列表_persons內的人的屬性時,都會重新評估。爲此,您必須執行一個自定義實現,以偵聽原始列表中每個PersonPropertyChanged事件並相應地更新ObservableCollection<Person>。這個可觀察的集合可以綁定到列表視圖。

相關問題