用户控件中的依赖项属性没有任何作用

用户控件中的依赖项属性没有任何作用

问题描述:

在我的用户控件(类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); } 
} 

但是,当根据下面进行设置时,不会给我任何行为上的差异。事实上,财产中的二传手并没有被调用。我错过了什么?

<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}}"/>