1

Recently started studying C#, making a project using AvaloniaUI, ReactiveUI and a MVVM pattern.

The essence of the problem is as follows: there is an AvaloniaList(observable collection), a ListBox is bound to it by binding.

MainWindow.axaml:

 <ListBox Items="{Binding Devices}" 
     SelectedItem="{Binding SelectedDevice}" 
     SelectionMode="Single, Toggle">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel Margin="5">
         <TextBlock FontSize="18" Text="{Binding name}" />
         <TextBlock Text="{Binding state}" />
         <TextBlock Text="{Binding ip}" />
       </StackPanel>
     </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

ViewModels:

private AvaloniaList<Device>? devices;
public AvaloniaList<Device> Devices
{
  get => devices;
  set
  {
    devices = value;
    this.RaiseAndSetIfChanged(ref devices, value);
  }
}

How can I make a ListBox update when the field of a certain AvaloniaList element changes?

So far I see only one option: to clear and re-fill the collection, but I would like to avoid this. 
joraobj
  • 11
  • 3

1 Answers1

0

if a device name changed it should be updated automatically in case the Device class also implement the INotifyPropertyChanged so check if "Name" property in "Device" Class has RaiseAndSetIfChanged

edit: if you can't change the class Device you need to make sure to raise property changed on the Devices list when updating a value

ZSH
  • 905
  • 5
  • 15
  • This will work if binding to a specific class field All fields of the class implement RaiseAndSetIfChanged, but the list is updated only when an element is added or removed, and not when it is changed Now I am deleting the element and putting the modified one in its place, but this is not a solution, but a crutch – joraobj Mar 14 '22 at 12:10
  • how about my edit suggestion ?, call notifypropertychanged on devices (not if changed) – ZSH Mar 15 '22 at 07:29