I have a few issues with binding an ObservableCollection using WinUI 3:
(a) When the collection changes (an item is added, removed), the ListView is not updated.
(b) Upon reordering the ListView (using the control itself), the ListView does not keep the new order.
Class Definitions
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Xml.Linq;
public class Standards
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private ObservableCollection<Standard> _Collection;
public ObservableCollection<Standard> Collection {
get => _Collection;
set { if (_Collection != value) { _Collection = value;
NotifyPropertyChanged("Description"); } }
}
}
public class Standard : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public Standard() { }
public Standard(XElement e)
{
Name = e.Attribute("name").Value;
Description = e.Attribute("description").Value;
}
public Standard(string standard, string description)
{
Name = standard;
Description = description;
}
private string _Description;
private string _name;
public string Name { get => _name; set { if (_name != value){ _name = value; NotifyPropertyChanged("Name"); } }
}
public string Description { get => _Description; set { if (_Description != value) { _name = value; NotifyPropertyChanged("Description"); } } }
}
WinUI XAML
<ListView x:Name="lvStandards"
Grid.Row="2"
Margin="5"
CanReorderItems="True"
ReorderMode="Enabled"
AllowDrop="True"
ItemsSource="{x:Bind viewModel.Standards.Collection, Mode=TwoWay}"/>