Home > Net >  MVVM Observable collection issue
MVVM Observable collection issue

Time:09-13

I am creating A WPF application and am currently having issues with updating the visuals for a particular instance.

I have a ViewModel.Textlines which I am trying to change one element within that. It works and behaves fine from what I gather.

I am using Remove and Insert and it doesnt work. I use breakpoints and find out it all seems to work and the element has indeed been swapped out and when I check the OC it will show the values I wanted it to have and OnPropertyChanged Has been called after but it fails to update that. It might occure because the value is not a 'new' MFD_textline as I am just assigning it via =

Below is the code I am using

private void Controller_UpdateEvent(object sender, UpdateEventArgs e)
{
    if(e.SystemName == "ALE47")
    {
        if(e.Values.ContainsKey("PageIndex") && e.Values.ContainsKey("Value") && e.Values.ContainsKey("TextLine"))
        {
            int pageIndex = (int)e.Values["PageIndex"].NewValue;
            int value = (int)e.Values["Value"].NewValue;
            textLine = new MFD_Textline();
            textLine = (MFD_Textline)e.Values["TextLine"].NewValue;
            ViewModel.TextLines.RemoveAt(value);
            ViewModel.TextLines.Insert(value, textLine);
        }
    }
}
public void TextLineChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    // when the textlines obserevable collection is changed (added to or removed) force a property changed to update the view
    if (e.Action == NotifyCollectionChangedAction.Add)
    {
        if (TextLines.Count == 11)
        {
            OnPropertyChanged("textLines");
        }
        
    }
}
private ObservableCollection<MFD_Textline> textLines;
public ObservableCollection<MFD_Textline> TextLines
{
    get { return textLines; }
    set
    {
        textLines = value;
        OnPropertyChanged("textlines");
    }
}

Please let me know if there are any questions or if I have not quite explained it right.

CodePudding user response:

This call is wrong OnPropertyChanged("textlines");

You should pass the Property's name, not the backing field's
OnPropertyChanged("TextLines");

  • Related