Tilak
Tilak

Reputation: 30728

Is derived type also becomes owner for dependency property defined in base class (in WPF/XAML)

In one of the module, I have seen following style being set.

<Style TargetType="Rectangle">
            <Style.Triggers>
                <EventTrigger RoutedEvent="MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation To="300" Duration="0:0:1.5" 
                    AccelerationRatio="0.10" DecelerationRatio="0.25" 
                    Storyboard.TargetProperty="(Canvas.Width)" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>

Note that TargetType is Rectangle while, Storyboard.TargetProperty is Canvas.Width. The style/trigger still is working correctly. It is animating the Rectangle.width property.

I understand that In Storyboard.TargetProperty (or anywhere else in XAML), we have to use PropertyPath syntax, which is like (ownerType.PropertyName).

My question is how setting animation on Canvas.Width is animating Rectangle.Width

  1. Is it because Canvas.Width, Rectangle.Width or FrameworkElement.Width all points to FrameowrkElement.Width property, because Width is defined in FrameworkElement, and Canvas/Rectangle are derived from it.
  2. Or Is it because, by virtue of Inheritance, Canvas and Rectangle both also becomes owner of the dependency property.

Upvotes: 5

Views: 831

Answers (2)

Johannes Wanzek
Johannes Wanzek

Reputation: 2875

As the Width Property is a Property of the FrameworkElement Class you can enter every Control which inherits from FrameworkElement

Storyboard.TargetProperty="(FrameworkElement.Width)"

But my favorite is:

Storyboard.TargetProperty="(DataGrid.Width)"

Try it. It will work!

Upvotes: 0

Kent Boogaart
Kent Boogaart

Reputation: 178760

It's because the XAML compiler resolves Canvas.Width by looking for a static field called WidthProperty on Canvas. Since Canvas inherits from FrameworkElement, a reference to Canvas.WidthProperty resolves to FrameworkElement.WidthProperty.

Because Rectangle also inherits from FrameworkElement, animating Canvas.WidthProperty is the same as animating Rectangle.WidthProperty, is the same as animating FrameworkElement.WidthProperty.

Upvotes: 1

Related Questions