简体   繁体   English

对 WPF 控件的动态定义内容进行动画处理

[英]Animate dynamically-defined content of a WPF control

I need to create a UserControl with variable appearance but common behavior.我需要创建一个外观可变但行为常见的UserControl Ie appearance can be different: a Square , a Polygon , or an Ellipse .即外观可以不同: SquarePolygonEllipse But the control needs to be able to animate (fade in and out) the said Square / Polygon / Ellipse .但是控件需要能够为所述Square / Polygon / Ellipse设置动画(淡入和淡出)。

I can create three different controls and define a Storyboard in each, and point the Storyboard to whatever Shape I have inside my control.我可以创建三个不同的控件并在每个控件中定义一个Storyboard ,并将 Storyboard 指向我在控件中的任何形状。 But I want to define the Storyboard only once to follow the DRY principle.但我只想定义一次 Storyboard 以遵循 DRY 原则。 So I created a control without content like this and want to point the storyboards to whatever content it will have:所以我创建了一个没有这样内容的控件,并希望将情节提要指向它将具有的任何内容:

<UserControl x:Class="wpf_test.StarControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:wpf_test"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Storyboard x:Key="FadeInStoryboard">
            <DoubleAnimation Storyboard.TargetName="?????"
                             Storyboard.TargetProperty="Opacity"
                             Duration="0:0:2"
                             From="0"
                             To="1"/>
        </Storyboard>
        <Storyboard x:Key="FadeOutStoryboard">
            <DoubleAnimation Storyboard.TargetName="?????"
                             Storyboard.TargetProperty="Opacity"
                             Duration="0:0:2"
                             From="1"
                             To="0"/>
        </Storyboard>
    </UserControl.Resources>
    

</UserControl>

I can then put whatever Shape I need inside that control, eg using a Syle with Template setter, but my problem I don't know how to point my Storyboard s to that future Shape, ie what to put into the Storyboard.TargetName="?????"然后我可以将我需要的任何形状放入该控件中,例如使用带有Template设置器的Syle ,但我的问题我不知道如何将我的Storyboard指向未来的形状,即放入Storyboard.TargetName="?????" instead of the question marks.而不是问号。

As per @Andy's and @EldHasp's comments, the solution was to add a name on the UserControl and animate the Opacity of the UserControl itself.根据@Andy 和@EldHasp 的评论,解决方案是在 UserControl 上添加一个名称并为 UserControl 本身的不透明度设置动画。

<UserControl x:Class="wpf_test.StarControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:wpf_test"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300"
             x:Name="ThisStarControl">
    <UserControl.Resources>
        <Storyboard x:Key="FadeInStoryboard">
            <DoubleAnimation Storyboard.TargetName="ThisStarControl"
                             Storyboard.TargetProperty="Opacity"
                             Duration="0:0:2"
                             From="0"
                             To="1"/>
        </Storyboard>
        <Storyboard x:Key="FadeOutStoryboard">
            <DoubleAnimation Storyboard.TargetName="ThisStarControl"
                             Storyboard.TargetProperty="Opacity"
                             Duration="0:0:2"
                             From="1"
                             To="0"/>
        </Storyboard>
    </UserControl.Resources>
    
</UserControl>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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