简体   繁体   English

根据另一个 ItemsControl 的选定对象更新 ItemsControl.ItemsSource

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

相关问题 显示ItemsControl.ItemsSource是否为null - Show if ItemsControl.ItemsSource is null ItemsControl.ItemsSource,绑定无效 - ItemsControl.ItemsSource, binding doesn't work 如何将ItemsControl.ItemsSource与XAML中的属性绑定? - How can I bind an ItemsControl.ItemsSource with a property in XAML? WPF ItemsControl:从另一个ItemsControl的当前选择中更改ItemsControl的ItemsSource - WPF ItemsControl: Change ItemsSource of ItemsControl from Current Selection of another ItemsControl ItemsControl不会更新ItemsSource绑定 - ItemsControl does not update the ItemsSource binding 使用 ItemsSource 时操作无效。 改为使用 ItemsControl.ItemsSource 访问和修改元素 - Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead 使用ItemsSource时,操作无效。 在执行两次时,使用ItemsControl.ItemsSource访问和修改元素 - Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead when execution twice 使用ItemsSource时,操作无效。 使用ItemsControl.ItemsSource访问和修改元素 - Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource ItemsControl.ItemsSource 如何从没有实现 IEnumerable 的东西绑定? - How does ItemsControl.ItemsSource bind from something that doesn't implement IEnumerable? 将数据绑定到ItemsControl内的ItemsSource - DataBinding an ItemsSource inside an ItemsControl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM