[英]C#/WINUI3: Data binding becomes invalid after using the OrderBy() to an ObservableCollection
Part of the.xaml:部分.xaml:
...
<ListView x:Name="DeviceList" ItemsSource="{x:Bind ViewModel.FliteredDevicesCollection,Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local1:BleDevice">
...
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
...
I created a custom class named BleDivice
, and I bind the ItemsSource of the ListView to ObservableCollection<BleDevice> FliteredDevicesCollection
to show the devices in the collection.我创建了一个名为
BleDivice
的自定义 class ,并将 ListView 的 ItemsSource 绑定到ObservableCollection<BleDevice> FliteredDevicesCollection
以显示集合中的设备。 The device list can be updated with the collection dynamically, which worked well.设备列表可以随集合动态更新,效果很好。
Then I tried to reorder the devices in the collection I used FliteredDevicesCollection = new ObservableCollection<BleDevice>(FliteredDevicesCollection.OrderBy(BleDevice => BleDevice.SignalStrength));
然后我尝试重新排序我使用的集合中的设备
FliteredDevicesCollection = new ObservableCollection<BleDevice>(FliteredDevicesCollection.OrderBy(BleDevice => BleDevice.SignalStrength));
, however, the bind became invalid. ,但是,绑定变得无效。
I printed the contents in the collection through Debug and found that it was indeed reordered, but the items in the listview no longer changed with the data in the collection.我通过Debug打印了集合中的内容,发现确实是重新排序了,但是listview中的项目不再随着集合中的数据而改变。 It seemed that the data binding had failed.
数据绑定似乎失败了。
Is it caused by this method OrderBy()
?它是由这种方法
OrderBy()
引起的吗? I tried the indirect way, but it still didn't solve the problem.我尝试了间接方式,但仍然没有解决问题。 So what is the reason and how to solve this problem, instead of using methods such as
DeviceList.ItemsSource = ViewModel.FliteredDevicesCollection;
那么是什么原因以及如何解决这个问题,而不是使用
DeviceList.ItemsSource = ViewModel.FliteredDevicesCollection;
等方法in the.xaml.cs file after each reordering to rebind the data.在.xaml.cs文件中每次重新排序后重新绑定数据。
New新的
private ObservableCollection<BleDevice> flierDevicesCollection { get; set; } = new();
public ObservableCollection<BleDevice> FliteredDevicesCollection
{
get => flierDevicesCollection;
set
{
flierDevicesCollection = value;
OnPropertyChanged("FliteredDevicesCollection");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
I have already added the code above, but it doesn't work.我已经添加了上面的代码,但它不起作用。 It is strange that I can add objects to the collection or remove objects from the collection
FliteredDevicesCollection.Add(bleDevice);
奇怪的是,我可以将对象添加到集合中或从集合中删除对象
FliteredDevicesCollection.Add(bleDevice);
, the ListView is updated normally, but I can't reorder the objects in the collection. , ListView 更新正常,但我无法重新排序集合中的对象。 These should all send notifications.
这些都应该发送通知。 Actually, the reorder progress has been done, but the binding between them failed, and the result couldn't be shown in the ListView.
实际上,reorder 的进度已经完成,但是它们之间的绑定失败,结果无法在 ListView 中显示。 So what's the reason?
那么是什么原因呢?
You don't show how you define your FliteredDevicesCollection
property, but taking a wild guess I'd say it doesn't notify on property changes, which is correct.您没有展示如何定义
FliteredDevicesCollection
属性,但大胆猜测我会说它不会通知属性更改,这是正确的。 What you're doing is a lazy anti-pattern.你正在做的是一个懒惰的反模式。
What you should be doing is to bind to a CollectionViewSource
, and set up filtering and/or ordering using the respective properties of that object.您应该做的是绑定到
CollectionViewSource
,并使用该 object 的相应属性设置过滤和/或排序。 That is literally why that object exists in all XAML frameworks.这就是为什么 object 存在于所有 XAML 框架中的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.