【问题标题】:Dependency property in user control doesn't have any effect用户控件中的依赖属性没有任何作用
【发布时间】:2016-02-29 20:39:30
【问题描述】:

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

也许我的绑定不正确?

【问题讨论】:

    标签: c# wpf xaml binding user-controls


    【解决方案1】:

    给你的UserControl命名:

    <UserControl .... x:Name="labeledBox">
    

    并像这样使用绑定:

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

    【讨论】:

      【解决方案2】:

      是的,您的 Binding 不太好尝试将其更新为:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-04
        • 2014-10-09
        • 1970-01-01
        相关资源
        最近更新 更多