[英]Update ItemsControl.ItemsSource based on selected object of a another ItemsControl
We use ReactiveUI and DynamicData and have two ListBoxes: ListBoxA and ListBoxB .我们使用 ReactiveUI 和 DynamicData 并有两个 ListBoxes: ListBoxA和ListBoxB 。 Based on the selection of ListBoxA , the list in ListBoxB should be updated (Not filtered).基于ListBoxA的选择,在ListBoxB名单应该被更新(不过滤)。 Seems straightforward but I am having some trouble refreshing ListBoxB看起来很简单,但我在刷新ListBoxB时遇到了一些麻烦
The ListBoxes are bound like so in the View:列表框在视图中像这样绑定:
this.OneWayBind(ViewModel, vm => vm.ItemsA, v => v.ListBoxA.ItemsSource).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedItemA, v => v.ListBoxA.SelectedItem).DisposeWith(disposables);
this.OneWayBind(ViewModel, vm => vm.ItemsB, v => v.ListBoxB.ItemsSource).DisposeWith(disposables);
ViewModel:=视图模型:=
_storage.PoolA.Connect()
.Transform(m => new ViewModelForA(m))
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(out _itemsA)
.Subscribe();
SelectedItemA.PoolB.Connect()
.Transform(c => new ViewModelForB(c))
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(out _itemsB)
.Subscribe();
Any help would be much appreciated!任何帮助将非常感激!
Every time you select something in ListBoxA
your SelectedItemA
object changes and you need to recreate you connection to it.每次您在ListBoxA
选择某些内容时,您的SelectedItemA
对象都会发生变化,您需要重新创建与它的连接。
At first we create an IObservable from SelectedItemA and then use Switch operator from DynamicData首先我们从 SelectedItemA 创建一个 IObservable,然后使用 DynamicData 的 Switch 操作符
class MyViewModel: INotifyPropertyChange
{
public MyViewModel()
{
_storage.PoolA.Connect()
...
this.WhenPropertyChanged(x => x.SelectedItemA)
.Select(x => x.Value.PoolB)
.Switch() // Switch from DynamicData
.Transform(c => new ViewModelForB(c))
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(out _itemsB)
.Subscribe();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.