简体   繁体   English

属性更改时c#ListView不更新

[英]c# ListView not updating when Property Changed

My UI is not updating when more data is added to the ObservableCollection. 将更多数据添加到ObservableCollection时,我的UI并未更新。 The console output says A first chance exception of type 'System.NullReferenceException' occurred. 控制台输出显示类型为'System.NullReferenceException'的第一次机会异常。 Should I be using Inotifycollectionchanged instead? 我应该改为使用Inotifycollectionchanged吗? Here is some of the code: 以下是一些代码:

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

UserConversationModel.cs 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 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 GrabUserConversationModel.cs

class GrabUserConversationModel
{

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


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

your property ObservableCollection<UserConversationModel> Conversation is not implementing the INotifyPropertyChanged 您的属性ObservableCollection<UserConversationModel> Conversation未实现INotifyPropertyChanged

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM