我可以在没有 DependencyProperty 的情况下连接两个 ViewModels 吗?
Can I connect two ViewModels without DependencyProperty?
我有 MainWindow(为清楚起见进行了简化):
<Window>
<!-- (...) -->
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
<!-- (...) -->
<CheckBox IsChecked="{Binding ShowAdvanced}" Content="Advanced view" />
<uc:MyUserControl DataContext={Binding MyUserControlViewModel} />
</Window>
MainWindowViewModel:
public partial class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel()
{
MyUserControlVM = new MyUserControlViewModel();
}
private bool _showAdvanced;
public bool ShowAdvanced
{
get => _showAdvanced;
set { _showAdvanced = value; NotifyPropertyChanged(); }
}
private MyUserControlViewModel _myUserControlVM;
public MyUserControlViewModel MyUserControlVM
{
get => _myUserControlVM;
set { _myUserControlVM= value; NotifyPropertyChanged(); }
}
}
在我的 UserControl 中,我有一些控件应该在 "Show advanced" 复选框未选中时隐藏。
<GroupBox Header="Some advanced stuff"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.(vm:MainWindowViewModel.ShowAdvanced), Converter={StaticResource BoolToVis}}">
<!-- (...) -->
</GroupBox>
这确实有效,但我不喜欢这样,因为 UserControl 依赖于 MainWindow。
如何在没有 DependencyProperty 的情况下正确连接这些视图模型?
我已尝试将此添加到 MyUserControlViewModel:
public MyUserControlViewModel(MainWindowViewModel parent)
{
Parent = parent;
}
private MainWindowViewModel _parent;
public MainWindowViewModel Parent
{
get { return _parent; }
set { _parent = value; NotifyPropertyChanged(); }
}
并像这样在 MyUserControl 控件之一上绑定可见性:
Visibility="{Binding Parent.ShowAdvanced}"
但这不起作用(MyUserControl 没有收到通知?)。
将 ShowAdvanced
属性 添加到控制 VM,并在将新值分配给 MainViewModel
ShowAdvanced
[=16 时将值分配给每个控制 VM =].
public class MainViewModel : Base.ViewModelBase
{
private bool _showAdvanced;
public MainViewModel()
{
MyUserControl1 = new MyUserControlViewModel { Message = "Control 1" };
MyUserControl2 = new MyUserControlViewModel { Message = "Control 2" };
MyUserControl3 = new MyUserControlViewModel { Message = "Control 3" };
}
public bool ShowAdvanced
{
get => _showAdvanced;
set
{
this.RaiseAndSetIfChanged( ref _showAdvanced, value );
MyUserControl1.ShowAdvanced = value;
MyUserControl2.ShowAdvanced = value;
MyUserControl3.ShowAdvanced = value;
}
}
public MyUserControlViewModel MyUserControl1 { get; }
public MyUserControlViewModel MyUserControl2 { get; }
public MyUserControlViewModel MyUserControl3 { get; }
}
public class MyUserControlViewModel : Base.ViewModelBase
{
private bool _showAdvanced;
private string _message;
public bool ShowAdvanced { get => _showAdvanced; set => this.RaiseAndSetIfChanged( ref _showAdvanced, value ); }
public string Message { get => _message; set => this.RaiseAndSetIfChanged( ref _message, value ); }
}
我有 MainWindow(为清楚起见进行了简化):
<Window>
<!-- (...) -->
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
<!-- (...) -->
<CheckBox IsChecked="{Binding ShowAdvanced}" Content="Advanced view" />
<uc:MyUserControl DataContext={Binding MyUserControlViewModel} />
</Window>
MainWindowViewModel:
public partial class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel()
{
MyUserControlVM = new MyUserControlViewModel();
}
private bool _showAdvanced;
public bool ShowAdvanced
{
get => _showAdvanced;
set { _showAdvanced = value; NotifyPropertyChanged(); }
}
private MyUserControlViewModel _myUserControlVM;
public MyUserControlViewModel MyUserControlVM
{
get => _myUserControlVM;
set { _myUserControlVM= value; NotifyPropertyChanged(); }
}
}
在我的 UserControl 中,我有一些控件应该在 "Show advanced" 复选框未选中时隐藏。
<GroupBox Header="Some advanced stuff"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.(vm:MainWindowViewModel.ShowAdvanced), Converter={StaticResource BoolToVis}}">
<!-- (...) -->
</GroupBox>
这确实有效,但我不喜欢这样,因为 UserControl 依赖于 MainWindow。
如何在没有 DependencyProperty 的情况下正确连接这些视图模型?
我已尝试将此添加到 MyUserControlViewModel:
public MyUserControlViewModel(MainWindowViewModel parent)
{
Parent = parent;
}
private MainWindowViewModel _parent;
public MainWindowViewModel Parent
{
get { return _parent; }
set { _parent = value; NotifyPropertyChanged(); }
}
并像这样在 MyUserControl 控件之一上绑定可见性:
Visibility="{Binding Parent.ShowAdvanced}"
但这不起作用(MyUserControl 没有收到通知?)。
将 ShowAdvanced
属性 添加到控制 VM,并在将新值分配给 MainViewModel
ShowAdvanced
[=16 时将值分配给每个控制 VM =].
public class MainViewModel : Base.ViewModelBase
{
private bool _showAdvanced;
public MainViewModel()
{
MyUserControl1 = new MyUserControlViewModel { Message = "Control 1" };
MyUserControl2 = new MyUserControlViewModel { Message = "Control 2" };
MyUserControl3 = new MyUserControlViewModel { Message = "Control 3" };
}
public bool ShowAdvanced
{
get => _showAdvanced;
set
{
this.RaiseAndSetIfChanged( ref _showAdvanced, value );
MyUserControl1.ShowAdvanced = value;
MyUserControl2.ShowAdvanced = value;
MyUserControl3.ShowAdvanced = value;
}
}
public MyUserControlViewModel MyUserControl1 { get; }
public MyUserControlViewModel MyUserControl2 { get; }
public MyUserControlViewModel MyUserControl3 { get; }
}
public class MyUserControlViewModel : Base.ViewModelBase
{
private bool _showAdvanced;
private string _message;
public bool ShowAdvanced { get => _showAdvanced; set => this.RaiseAndSetIfChanged( ref _showAdvanced, value ); }
public string Message { get => _message; set => this.RaiseAndSetIfChanged( ref _message, value ); }
}