简体   繁体   English

C#/WINUI3:使用 OrderBy() 到 ObservableCollection 后数据绑定变得无效

[英]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.

相关问题 (winui3 with c#)更改绑定的 Xmal UI 元素的源数据,无法在此 UI 元素上显示 - (winui3 with c#)change source data of Xmal UI elements which binded, can not show on this UI element WinUI3 TabView XAML 不更新视图中的数据绑定 - WinUI3 TabView XAML does not update the data binding in the view UWP/WinUI3 如何获取C#中的系统主题? - UWP/WinUI3 How to get system theme in C#? C#和WPF数据绑定到ObservableCollection - C# & WPF Data Binding to ObservableCollection x:Bind in WinUI3 绑定到日期时间 - x:Bind in WinUI3 Binding to a datetime C# WinUI3 桌面:如何引用或控制 ListView 的所选项目 DataTemplate 中的按钮? - C# WinUI3 Desktop: How can I reference or control a button in a ListView's selected item DataTemplate? 如何从 WinUI3 C# 应用程序写入 HKEY_CURRENT_USER\\Control Panel\\Desktop\\WindowMetrics - How to write to HKEY_CURRENT_USER\\Control Panel\\Desktop\\WindowMetrics from WinUI3 C# app 保存一个数组到 Windows.Storage.ApplicationData.Current.LocalSettings (C# + WinUI3) - Save an array to Windows.Storage.ApplicationData.Current.LocalSettings (C# + WinUI3) 使用C#WPF中的Linq-to-SQL将嵌套ListView绑定到ObservableCollection的数据 - Data Binding a Nested ListView to an ObservableCollection using Linq-to-SQL in C# WPF 在C#中绑定DataGrid和ObservableCollection - Binding DataGrid and ObservableCollection in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM