C# 将自定义userControl属性与DependencyProperty绑定
我正在尝试使用DependencyProperty绑定自定义UserControl属性 这是我的userControl,值是我要绑定的属性:C# 将自定义userControl属性与DependencyProperty绑定,c#,wpf,binding,dependency-properties,C#,Wpf,Binding,Dependency Properties,我正在尝试使用DependencyProperty绑定自定义UserControl属性 这是我的userControl,值是我要绑定的属性: public partial class MyCustomUserControl: UserControl { bool _value = false; public bool Value { get { return _value; } set
public partial class MyCustomUserControl: UserControl
{
bool _value = false;
public bool Value
{
get
{
return _value;
}
set
{
SetState(value);
}
public static DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(bool), typeof(MyCustomUserControl));
}
这里是viewModel,INotifyPropertyChanged和公共属性CustomViewModelValueProperty:
public class CustomViewModel: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged == null) { return; }
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
protected void SetField<T>(ref T field, T value, string propertyName)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return;
field = value;
OnPropertyChanged(propertyName);
}
private bool _viewModelValue;
public bool CustomViewModelValueProperty
{
get { return _viewModelValue; }
set { SetField(ref _viewModelValue, value,nameof(CustomViewModelValueProperty)); }
}
包含带有绑定的MyCustomUserControl的XAML:
<CustomWindow:MyCustomUserControl x:Name="UserControl1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="48" Width="48" Value="{Binding Path=CustomViewModelValueProperty, Mode=OneWay}"/>
此时,当我强制PropertyChanged时,它有一个订阅,但MyCustomUserControl中的“值”设置程序没有被调用,我缺少什么
谢谢 您的依赖项属性声明错误。
<CustomWindow:MyCustomUserControl x:Name="UserControl1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="48" Width="48" Value="{Binding Path=CustomViewModelValueProperty, Mode=OneWay}"/>