简体   繁体   中英

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

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. 

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

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