简体   繁体   中英

Dependency property in user control doesn't have any effect

In my user control (class LabeledBox ), I've added dependency property as follows.

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

However, when setting it according to the below, doesn't give me any difference in behavior. In fact, the setter in the property doesn't get called. What do I miss?

<local:LabeledBox x:Name="Info field" 
                  Description="Info" 
                  Horizontal="Horizontal" />

The component in question has a stack panel as outermost control and it's bound like this.

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

Perhaps I've done the binding incorrectly?

Give a name to your UserControl :

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

And use the binding like this:

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

Yes your Binding is not well try to update it to be seems like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM