2011-11-04 103 views
1

我是WPF的noob。希望我能找到答案。Bound DataGrid沒有更新集合變更

例如,我有一個List<Customer>,它綁定到dataGrid。如果我添加一個新的客戶到列表中,並且我沒有看到datagrid在那裏添加一個新行。

<DataGrid ItemsSource="{Binding Customers}" AutoGenerateColumns="False" Height="318"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="First Name" Binding="{Binding FirstName, Mode=TwoWay}" /> 
     <DataGridTemplateColumn Header="Image" Width="SizeToCells" IsReadOnly="True"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <Image Height="80" Source="{Binding Image,Mode=TwoWay}" /> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ComponentModel; 
using System.Windows.Data; 

namespace DataGrid 
{ 
    public class MainWindowViewModel 
    { 
     public ICollectionView Customers { get; private set; } 
     public ICollectionView GroupedCustomers { get; private set; } 
     public List<Customer> _customers { get; set; } 

     public MainWindowViewModel() 
     { 
      _customers = new List<Customer> 
          { 
           new Customer 
            { 
             FirstName = "Christian", 
             LastName = "Moser", 
             Gender = Gender.Male, 
             WebSite = new Uri("http://www.wpftutorial.net"), 
             ReceiveNewsletter = true, 
             Image = "Images/christian.jpg" 
            }, 
           new Customer 
            { 
             FirstName = "Peter", 
             LastName = "Meyer", 
             Gender = Gender.Male, 
             WebSite = new Uri("http://www.petermeyer.com"), 
             Image = "Images/peter.jpg" 
            }, 
           new Customer 
            { 
             FirstName = "Lisa", 
             LastName = "Simpson", 
             Gender = Gender.Female, 
             WebSite = new Uri("http://www.thesimpsons.com"), 
             Image = "Images/lisa.jpg" 
            }, 
           new Customer 
            { 
             FirstName = "Betty", 
             LastName = "Bossy", 
             Gender = Gender.Female, 
             WebSite = new Uri("http://www.bettybossy.ch"), 
             Image = "Images/betty.jpg" 
            } 
          }; 
      Customers = CollectionViewSource.GetDefaultView(_customers); 
      GroupedCustomers = new ListCollectionView(_customers); 
      GroupedCustomers.GroupDescriptions.Add(new PropertyGroupDescription("Gender"));   
     } 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Reflection; 

namespace DataGrid 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     private new MainWindowViewModel view { get; set; } 
     public MainWindow() 
     { 
      InitializeComponent(); 
      view = new MainWindowViewModel(); 
      DataContext = view; 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      view._customers.Add(new Customer 
           { 
            FirstName = "Lei", 
            LastName = "Moser", 
            Gender = Gender.Male, 
            WebSite = new Uri("http://www.wpftutorial.net"), 
            ReceiveNewsletter = true, 
            Image = "Images/christian.jpg" 
           }); 
     } 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ComponentModel; 

namespace DataGrid 
{  
    public enum Gender 
    { 
     Male, 
     Female 
    } 

    public class Customer : INotifyPropertyChanged 
    { 
     private string _firstName; 
     private string _lastName; 
     private Gender _gender; 
     private Uri _webSite; 
     private bool _newsletter; 
     private string _image; 

     public string FirstName 
     { 
      get { return _firstName; } 
      set 
      { 
       _firstName = value; 
       NotifyPropertyChanged("FirstName"); 
      } 
     } 

     public string LastName 
     { 
      get { return _lastName; } 
      set 
      { 
       _lastName = value; 
       NotifyPropertyChanged("LastName"); 
      } 
     } 

     public Gender Gender 
     { 
      get { return _gender; } 
      set 
      { 
       _gender = value; 
       NotifyPropertyChanged("Gender"); 
      } 
     } 

     public Uri WebSite 
     { 
      get { return _webSite; } 
      set 
      { 
       _webSite = value; 
       NotifyPropertyChanged("WebSite"); 
      } 
     } 

     public bool ReceiveNewsletter 
     { 
      get { return _newsletter; } 
      set 
      { 
       _newsletter = value; 
       NotifyPropertyChanged("Newsletter"); 
      } 
     } 

     public string Image 
     { 
      get { return _image; } 
      set 
      { 
       _image = value; 
       NotifyPropertyChanged("Image"); 
      } 
     } 

     #region INotifyPropertyChanged Members 
     public event PropertyChangedEventHandler PropertyChanged; 
     #endregion 

     #region Private Helpers 
     private void NotifyPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
     #endregion 
    } 
} 

我設置bindingMode到雙向,我有一個button_onClick,只是增加一個新客戶綁定的列表,但事情我沒有看到數據網格添加一個新行吧。
任何人都可以幫忙嗎?

回答

5

問題是List<T>沒有執行INotifyCollectionChanged。沒有這些,沒有任何東西告訴WPF已經添加了一個新項目。

這裏最好的選擇是將您的班級更改爲使用ObservableCollection<T>而不是List<T>。這將導致WPF自動收到通知,無論何時添加或從集合中刪除項目,都會進行適當的更新。

+0

是的,它的工作原理。謝謝。 ObservableCollection 和INotifyCollectionChanged有什麼不同? – retide

+0

@retide:'ObservableCollection '只是*實現* INotifyCollectionChanged'的框架中的一個集合。讓集合實現'INotifyCollectionChanged'是實現這個工作的關鍵。 –

0

您的List對象可能不是從INotiftyPropertyChanged繼承,因此它不會告訴您的程序更新對象。

如果您可以發佈一些代碼,我們可能可以確定是否是這種情況或導致問題的原因。

+0

問題實際上是'List '沒有實現'INotifyCollectionChanged' - 實現'INotifyPropertyChanged'不會糾正這個問題,除非OP在每次添加項目時都會爲列表引發屬性更改事件列表中每個元素的綁定,並且比必要的慢很多)。 –