C# 用户控件中的依赖项属性不为';没有任何效果
在我的用户控件(类LabeledBox)中,我添加了依赖属性,如下所示C# 用户控件中的依赖项属性不为';没有任何效果,c#,wpf,xaml,binding,user-controls,C#,Wpf,Xaml,Binding,User Controls,在我的用户控件(类LabeledBox)中,我添加了依赖属性,如下所示 public static readonly DependencyProperty HorizontalProperty = DependencyProperty.Register( "Horizontal", typeof (Orientation), typeof (LabeledBox), new PropertyMetadata(default(Orientation)))
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 .... x:Name="labeledBox">
然后像这样使用绑定:
<StackPanel Name="TheContainer" Orientation="{Binding Horizontal, ElementName=labeledBox}">
是的,您的绑定不好尝试将其更新为:
<StackPanel Name="TheContainer"
Orientation="{
Binding Horizontal,
RelativeSource={RelativeSource AncestorType=local:LabeledBox}}"/>