Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用户控件中的依赖项属性不为';没有任何效果_C#_Wpf_Xaml_Binding_User Controls - Fatal编程技术网

C# 用户控件中的依赖项属性不为';没有任何效果

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)))

在我的用户控件(类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 .... x:Name="labeledBox">

然后像这样使用绑定:

<StackPanel Name="TheContainer" Orientation="{Binding Horizontal, ElementName=labeledBox}">

是的,您的
绑定不好尝试将其更新为:

<StackPanel Name="TheContainer" 
            Orientation="{
              Binding Horizontal,
              RelativeSource={RelativeSource AncestorType=local:LabeledBox}}"/>