反应式UI - 大纲/细节绑定 - 取消选择当前记录



我正在尝试使用附件列表的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和一组控件来让用户编辑属性(例如,一个名为AttachmentNameTextBox)。以下是我根据上面的视图模型在视图中执行的操作:

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 表达式不会触发属性更改事件,因为绑定深度不止一个属性?

有什么办法吗?也许还有另一种方法可以实现主从导航/编辑?

您可以在视图中观察选定项并更改附件名称:

this.WhenAnyValue(x => x.ViewModel.Selected).Where(x => x == null)
.Subscribe(selected => AttachmentName.Text == "").DisposeWith(d);

最新更新