我尝试使用ReactiveUI
为附件列表实现主/详细信息。这是我的视图模型的简化版本:
public class AttachmentsViewModel : ReactiveObject, ISupportsActivation
{
private ReactiveList<Attachment> _attachments;
public ReactiveList<Attachment> Attachments
{
get => _attachments;
set => this.RaiseAndSetIfChanged( ref _attachments, value );
}
private IReactiveDerivedList<AttachmentViewModel> _attachmentViewModels;
public IReactiveDerivedList<AttachmentViewModel> AttachmentViewModels
{
get => _attachmentViewModels;
set => this.RaiseAndSetIfChanged(ref _attachmentViewModels, value );
}
private AttachmentViewModel _selected;
public AttachmentViewModel Selected
{
get => _selected;
set => this.RaiseAndSetIfChanged( ref _selected, value );
}
}
在我看来,我有ListView
和一组控件让用户编辑属性(例如TextBox
名为AttachmentName
。根据上面的视图模型,我将在视图中执行此操作:
public class AttachmentsView : IViewFor<AttachmentsViewModel>
{
public AttachmentsView()
{
InitializeComponent();
this.WhenActivated( d => {
this.OneWayBind( ViewModel, vm => vm.AttachmentViewModels, v => v.List.ItemsSource ).DisposeWith( d );
this.Bind( ViewModel, vm => vm.Selected, v => v.List.SelectedItem ).DisposeWith( d );
this.Bind( ViewModel, vm => vm.Selected.Name, v => v.AttachmentName.Text ).DisposeWith( d );
}
}
}
所有这些都按预期工作,当我点击ListView
中的某一行时,我面板中的控件会相应更改。
但是,当我取消选择当前选定的项目时,AttachmentName
仍会显示旧值(而不显示任何内容)。我假设linq表达式不会触发属性更改事件,因为绑定是多个属性深度?
这有什么办法吗?也许有另一种方法来实现主 - 细节导航/编辑?
答案 0 :(得分:0)
您可以在视图中观察SelectedItem并更改AttachmentName:
this.WhenAnyValue(x => x.ViewModel.Selected).Where(x => x == null)
.Subscribe(selected => AttachmentName.Text == "").DisposeWith(d);