简体   繁体   中英

c# ListView not updating when Property Changed

My UI is not updating when more data is added to the ObservableCollection. The console output says A first chance exception of type 'System.NullReferenceException' occurred. Should I be using Inotifycollectionchanged instead? 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

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; }
    }
     ...

your property ObservableCollection<UserConversationModel> Conversation is not implementing the INotifyPropertyChanged

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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