2015-02-07 79 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