用户控件中的依赖项 属性 没有任何效果
Dependency property in user control doesn't have any effect
在我的用户控件(class LabeledBox)中,我添加了如下依赖项属性。
public static readonly DependencyProperty HorizontalProperty
= DependencyProperty.Register(
"Horizontal",
typeof (Orientation),
typeof (LabeledBox),
new PropertyMetadata(default(Orientation)));
public Orientation Horizontal
{
get { return (Orientation) GetValue(HorizontalProperty); }
set { SetValue(HorizontalProperty, value); }
}
然而,当根据以下设置时,我的行为没有任何差异。事实上,属性 中的 setter 不会被调用。我想念什么?
<local:LabeledBox x:Name="Info field"
Description="Info"
Horizontal="Horizontal" />
有问题的组件有一个堆栈面板作为最外层的控件,它是这样绑定的。
<StackPanel Name="TheContainer" Orientation="{Binding Horizontal}">
可能是我绑定不正确?
为您的 UserControl
命名:
<UserControl .... x:Name="labeledBox">
并像这样使用绑定:
<StackPanel Name="TheContainer" Orientation="{Binding Horizontal, ElementName=labeledBox}">
是的,你的 Binding
不太好尝试将其更新为:
<StackPanel Name="TheContainer"
Orientation="{
Binding Horizontal,
RelativeSource={RelativeSource AncestorType=local:LabeledBox}}"/>
在我的用户控件(class LabeledBox)中,我添加了如下依赖项属性。
public static readonly DependencyProperty HorizontalProperty
= DependencyProperty.Register(
"Horizontal",
typeof (Orientation),
typeof (LabeledBox),
new PropertyMetadata(default(Orientation)));
public Orientation Horizontal
{
get { return (Orientation) GetValue(HorizontalProperty); }
set { SetValue(HorizontalProperty, value); }
}
然而,当根据以下设置时,我的行为没有任何差异。事实上,属性 中的 setter 不会被调用。我想念什么?
<local:LabeledBox x:Name="Info field"
Description="Info"
Horizontal="Horizontal" />
有问题的组件有一个堆栈面板作为最外层的控件,它是这样绑定的。
<StackPanel Name="TheContainer" Orientation="{Binding Horizontal}">
可能是我绑定不正确?
为您的 UserControl
命名:
<UserControl .... x:Name="labeledBox">
并像这样使用绑定:
<StackPanel Name="TheContainer" Orientation="{Binding Horizontal, ElementName=labeledBox}">
是的,你的 Binding
不太好尝试将其更新为:
<StackPanel Name="TheContainer"
Orientation="{
Binding Horizontal,
RelativeSource={RelativeSource AncestorType=local:LabeledBox}}"/>