2015-02-07 58 views
0

当更多数据添加到ObservableCollection时,我的UI未更新。控制台输出显示发生了类型'System.NullReferenceException'的第一次机会异常。我应该使用Inotifycollectionchanged吗?下面是一些代码:当Property Changed不更新时,ListView不会更新

<ListView x:Name="ListView2" ItemsSource="{Binding Source={x:Static d:GrabUserConversationModel._Conversation}, UpdateSourceTrigger=PropertyChanged}" SelectionChanged="ListView1_SelectionChanged"> 

UserConversationModel.cs

public class UserConversationModel : INotifyPropertyChanged 
{ 
    public UserConversationModel() 
    { 
    } 

    public string Name 
    { get; set; } 



    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(string Obj) 
    { 
     if (PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(Obj)); 
     } 
    } 
} 

MainWindow.xaml.cs

public partial class MainWindow 
{ 

    static GrabUserConversationModel grabUserConversationModel; 


    public MainWindow() 
    { 
     InitializeComponent(); 
     ... 

    } 

    static void AddData() 
    { 
    grabUserConversationModel.Conversation.Add(new UserConversationModel { Name = "TestName" }); 

    } 

GrabUserConversationModel.cs

class GrabUserConversationModel 
{ 

    public static ObservableCollection<UserConversationModel> _Conversation = new ObservableCollection<UserConversationModel>(); 


    public ObservableCollection<UserConversationModel> Conversation 
    { 
     get { return _Conversation; } 
     set { _Conversation = value; } 
    } 
    ... 
+0

尝试删除你的'UpdateSourceTrigger'上绑定设置。让WPF钩住集合并捕获集合更改,就像它通常默认的那样。 – TyCobb 2015-02-07 22:56:36

回答

0

你的财产ObservableCollection<UserConversationModel> Conversation不执行INotifyPropertyChanged

public ObservableCollection<UserConversationModel> Conversation 
{ 
    get { return _Conversation; } 
    set { _Conversation = value; OnPropertyChanged("Conversation");} 
} 
+0

'ObservableCollection '确实实现了'INotifyPropertyChanged'。 – TyCobb 2015-02-07 22:41:39

+0

显然我的意思是使用它的方法OnPropertyChanged :) – 2015-02-07 22:42:54

+1

这个解决方案的问题是,如果你看看他的绑定,他不是绑定到该属性,而是绑定到静态字段。 – TyCobb 2015-02-07 22:44:36