gpt4 book ai didi

c# - ReactiveUI - 将 ReactiveList 绑定(bind)到 ObservableCollection
转载 作者:太空宇宙 更新时间:2023-11-03 15:37:46 28 4
gpt4 key购买 nike

我在使用 ReactiveUI 绑定(bind) ReactiveList<object> 时遇到问题在我的 ViewModel 到我的 WPF View 中控件的 SelectedItems 属性( ObservableCollection<object> )。我试过:

this.Bind(ViewModel, vm => vm.SelectedItems, x => x.CBSelect.SelectedItems);

但是我得到了错误:

An exception of type 'System.ArgumentException' occurred in ReactiveUI.dll but was not handled in user code

Additional information: Can't two-way convert between ReactiveUI.ReactiveList[System.Object] and System.Collections.ObjectModel.ObservableCollection[System.Object]. To fix this, register a IBindingTypeConverter

我看到 Paul Betts 在另一篇文章中推荐了一种不同的方法:

 this.WhenAnyValue(x => x.countries.SelectedItems)
.Select(list => list.Cast<Country>())
.BindTo(this, x => x.ViewModel.SelectedCountries);

我试过了,但我得到了与第一个类似的错误:

An exception of type 'System.ArgumentException' occurred in ReactiveUI.dll but was not handled in user code

Additional information: Can't convert System.Collections.Generic.IEnumerable[System.Object] to ReactiveUI.ReactiveList[System.Object]. To fix this, register a IBindingTypeConverter

我做错了什么?非常感谢收到任何建议!

最佳答案

很抱歉这么晚才回答,但是你可以使用 ReactiveUI 做的事情是这样的。添加reactiveui-events- package对于您的平台,然后在 View 代码中添加:

this.WhenActivated(d => {
d(this.countries.Events()
.SelectionChanged
.Subscribe(ea => {
using (ViewModel.SelectedCountries.SuppressChangeNotifications())
{
ViewModel.SelectedCountries.RemoveAll(ea.RemovedItems.Cast<Country>());
ViewModel.SelectedCountries.AddRange(ea.AddedItems.Cast<Country>());
}
}));
});

通过这种方式,您将获得一个 react 列表,该列表会根据用户交互发生变化,并且您可以对 View 模型中的这些变化使用react。

关于c# - ReactiveUI - 将 ReactiveList<object> 绑定(bind)到 ObservableCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31250256/

28 4 0