简体   繁体   中英

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.

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 .

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!

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