无法绑定到页面上 UserControl 的自定义依赖项 属性
Binding to a custom Dependency Property of a UserControl on a Page isn't working
我创建了一个用户控件,它有一个自定义依赖项 属性(此 属性 未绑定到用户控件中)。我试图在我使用它的页面上绑定 属性 。在应用程序启动时,绑定有效,但之后我无法更新值。
绑定源的值正在变化,如果我在页面上绑定一个对象(例如文本块的文本 属性),我可以看到变化。
我的部分用户控件:
public int MaxValue
{
get { return (int)GetValue(MaxValueProperty); }
set
{
SetValue(MaxValueProperty, value);
NotifyPropertyChanged("MaxValue");
}
}
public static readonly DependencyProperty MaxValueProperty =
DependencyProperty.Register(
"MaxValue",
typeof(int),
typeof(NumericUpDown),
new PropertyMetadata(
5,
new PropertyChangedCallback(OnMaxValuePropertyChanged))
);
我页面的一部分:
<local:NumericUpDown x:Name="NudChapter"
Height="40"
Width="auto"
VerticalAlignment="Top"
Margin="0,10,0,0"
MaxValue="{Binding Path=Model.ChaptersInTheBook}"
HintText="Fejezet"/>
我解决了我的问题。我从我的用户控件 ctor
中删除了 DataContext=this
设置,现在它运行良好。
我创建了一个用户控件,它有一个自定义依赖项 属性(此 属性 未绑定到用户控件中)。我试图在我使用它的页面上绑定 属性 。在应用程序启动时,绑定有效,但之后我无法更新值。
绑定源的值正在变化,如果我在页面上绑定一个对象(例如文本块的文本 属性),我可以看到变化。
我的部分用户控件:
public int MaxValue
{
get { return (int)GetValue(MaxValueProperty); }
set
{
SetValue(MaxValueProperty, value);
NotifyPropertyChanged("MaxValue");
}
}
public static readonly DependencyProperty MaxValueProperty =
DependencyProperty.Register(
"MaxValue",
typeof(int),
typeof(NumericUpDown),
new PropertyMetadata(
5,
new PropertyChangedCallback(OnMaxValuePropertyChanged))
);
我页面的一部分:
<local:NumericUpDown x:Name="NudChapter"
Height="40"
Width="auto"
VerticalAlignment="Top"
Margin="0,10,0,0"
MaxValue="{Binding Path=Model.ChaptersInTheBook}"
HintText="Fejezet"/>
我解决了我的问题。我从我的用户控件 ctor
中删除了 DataContext=this
设置,现在它运行良好。