绑定到 ViewModel 中的依赖项 属性
Binding to dependency property in a ViewModel
我在 SliderViewModel 中有依赖项 属性,其中此视图模型实现 DependencyObject 并设置为 BRSliderUserControl 的数据上下文。如何从 AmplitudeOptionsUserControl 绑定到视图模型中的依赖项 属性。是否可以这样做。我的猜测是我需要在 BRSliderUserControl 中创建另一个依赖项 属性,然后将更新值发送到视图模型。这是正确的方法吗?
SliderViewModel.cs
public Class SliderViewModel:DependencyObject
{
public AnalysisViewType AnalysisTypeValue
{
get { return (AnalysisViewType)GetValue(AnalysisTypeDependencyProperty); }
set { SetValue(AnalysisTypeDependencyProperty, value); }
}
public static readonly DependencyProperty AnalysisTypeDependencyProperty =
DependencyProperty.Register("AnalysisTypeValue", typeof(AnalysisViewType), typeof(SliderViewModel),
new PropertyMetadata(AnalysisViewType.Unassigned, OnAnalysisTypeChanged));
private static void OnAnalysisTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//Do something here
}
}
BRSliderUserControl.xaml.cs
public BRSliderUserControl()
{
InitializeComponent();
SliderViewModel sliderViewModel = new SliderViewModel();
this.DataContext = sliderViewModel;
}
现在如何从另一个用户控件绑定到该依赖项 属性?
AmplitudeOptionsControl.xaml
//This does not work..
<lib:BRSliderUserControl
Grid.Row="5"
Grid.Column="0"
Grid.ColumnSpan="3"
AnalysisTypeValue="{Binding AmplitudeOptionsVM.AnalysisType,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
/>
您的视图模型中不需要依赖性 属性。您的 ViewModel 应该实现 INotifyPropertyChanged 接口,并且您的属性应该在值更改时引发 NotifyPropertyChanged 事件。周围有很多帮手,这让这更容易一些。
您可以根据需要使用 Dependency 属性,但这会使您的视图模型依赖于 WPF,尽管绑定到 Dependency 属性似乎要快得多(参见此处:https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/optimizing-performance-data-binding)
您可以在此处查看讨论:INotifyPropertyChanged vs. DependencyProperty in ViewModel
此外,由于您的 DataContext 是 SliderViewModel 类型,它有一个 public 属性 命名的 AnalysisTypeValue,在您的 XAML 中,您应该像这样绑定
... AnalysisTypeValue = {Binding AnalysisTypeValue}
将依赖项 属性 移动到 UserControl
class 的代码隐藏:
public class BRSliderUserControl
{
public AnalysisViewType AnalysisTypeValue
{
get { return (AnalysisViewType)GetValue(AnalysisTypeDependencyProperty); }
set { SetValue(AnalysisTypeDependencyProperty, value); }
}
public static readonly DependencyProperty AnalysisTypeDependencyProperty =
DependencyProperty.Register("AnalysisTypeValue", typeof(AnalysisViewType), typeof(BRSliderUserControl),
new PropertyMetadata(AnalysisViewType.Unassigned, OnAnalysisTypeChanged));
private static void OnAnalysisTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//Do something here
}
public BRSliderUserControl()
{
InitializeComponent();
SliderViewModel sliderViewModel = new SliderViewModel();
this.DataContext = sliderViewModel;
}
}
将普通 CLR 属性 添加到视图模型:
public class SliderViewModel : INotifyPropertyChanged
{
private AnalysisViewType _analysisTypeValue;
public AnalysisViewType AnalysisTypeValue
{
get { return _analysisTypeValue; }
set { _analysisTypeValue = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
将视图中的依赖项 属性 绑定到视图模型的源 属性:
<lib:BRSliderUserControl
Grid.Row="5"
Grid.Column="0"
Grid.ColumnSpan="3"
AnalysisTypeValue="{Binding AnalysisTypeValue}" />
我在 SliderViewModel 中有依赖项 属性,其中此视图模型实现 DependencyObject 并设置为 BRSliderUserControl 的数据上下文。如何从 AmplitudeOptionsUserControl 绑定到视图模型中的依赖项 属性。是否可以这样做。我的猜测是我需要在 BRSliderUserControl 中创建另一个依赖项 属性,然后将更新值发送到视图模型。这是正确的方法吗?
SliderViewModel.cs
public Class SliderViewModel:DependencyObject
{
public AnalysisViewType AnalysisTypeValue
{
get { return (AnalysisViewType)GetValue(AnalysisTypeDependencyProperty); }
set { SetValue(AnalysisTypeDependencyProperty, value); }
}
public static readonly DependencyProperty AnalysisTypeDependencyProperty =
DependencyProperty.Register("AnalysisTypeValue", typeof(AnalysisViewType), typeof(SliderViewModel),
new PropertyMetadata(AnalysisViewType.Unassigned, OnAnalysisTypeChanged));
private static void OnAnalysisTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//Do something here
}
}
BRSliderUserControl.xaml.cs
public BRSliderUserControl()
{
InitializeComponent();
SliderViewModel sliderViewModel = new SliderViewModel();
this.DataContext = sliderViewModel;
}
现在如何从另一个用户控件绑定到该依赖项 属性?
AmplitudeOptionsControl.xaml
//This does not work..
<lib:BRSliderUserControl
Grid.Row="5"
Grid.Column="0"
Grid.ColumnSpan="3"
AnalysisTypeValue="{Binding AmplitudeOptionsVM.AnalysisType,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
/>
您的视图模型中不需要依赖性 属性。您的 ViewModel 应该实现 INotifyPropertyChanged 接口,并且您的属性应该在值更改时引发 NotifyPropertyChanged 事件。周围有很多帮手,这让这更容易一些。
您可以根据需要使用 Dependency 属性,但这会使您的视图模型依赖于 WPF,尽管绑定到 Dependency 属性似乎要快得多(参见此处:https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/optimizing-performance-data-binding)
您可以在此处查看讨论:INotifyPropertyChanged vs. DependencyProperty in ViewModel
此外,由于您的 DataContext 是 SliderViewModel 类型,它有一个 public 属性 命名的 AnalysisTypeValue,在您的 XAML 中,您应该像这样绑定
... AnalysisTypeValue = {Binding AnalysisTypeValue}
将依赖项 属性 移动到 UserControl
class 的代码隐藏:
public class BRSliderUserControl
{
public AnalysisViewType AnalysisTypeValue
{
get { return (AnalysisViewType)GetValue(AnalysisTypeDependencyProperty); }
set { SetValue(AnalysisTypeDependencyProperty, value); }
}
public static readonly DependencyProperty AnalysisTypeDependencyProperty =
DependencyProperty.Register("AnalysisTypeValue", typeof(AnalysisViewType), typeof(BRSliderUserControl),
new PropertyMetadata(AnalysisViewType.Unassigned, OnAnalysisTypeChanged));
private static void OnAnalysisTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//Do something here
}
public BRSliderUserControl()
{
InitializeComponent();
SliderViewModel sliderViewModel = new SliderViewModel();
this.DataContext = sliderViewModel;
}
}
将普通 CLR 属性 添加到视图模型:
public class SliderViewModel : INotifyPropertyChanged
{
private AnalysisViewType _analysisTypeValue;
public AnalysisViewType AnalysisTypeValue
{
get { return _analysisTypeValue; }
set { _analysisTypeValue = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
将视图中的依赖项 属性 绑定到视图模型的源 属性:
<lib:BRSliderUserControl
Grid.Row="5"
Grid.Column="0"
Grid.ColumnSpan="3"
AnalysisTypeValue="{Binding AnalysisTypeValue}" />