【问题标题】:How to animate a TemplatedParent property inside a storyboard inside a controltemplate如何在控件模板内的情节提要中为 TemplatedParent 属性设置动画
【发布时间】:2018-02-09 17:06:44
【问题描述】:
<VisualState x:Name="MouseOver">
    <Storyboard>
         <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Bd" 
                                        Storyboard.TargetProperty="Fill">
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource 
                                        ButtonHoverBackgroundBrush}" />
          </ObjectAnimationUsingKeyFrames>
          <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Bd" 
                                         Storyboard.TargetProperty="Stroke">
               <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource 
                                                   ButtonHoverBorderBrush}" />
          </ObjectAnimationUsingKeyFrames>
          <ObjectAnimationUsingKeyFrames Duration="0" 
                                         Storyboard.TargetProperty="Foreground" 
                                         Storyboard.TargetName="ContentControl">
               <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource 
                                                   LightForegroundBrush}" />
         </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

而不是 Storyboard.TargetName=ContentControl 和 Storyboard.TargetProperty=Foreground, 我想为模板化父级的前景设置动画,并且内容控件只需将其前景属性绑定到模板化父级

Foreground={TemplateBinding Foreground}

原因是当我使用这个模板的控件时,我希望能够做到:

<Button Width="125"
        Height="30"
        Click="OnButtonClick"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Margin="50,54,0,0">
        <Button.Content>
            <controls:SomeCustomControl '
                             Foreground="{Binding Path=Foreground, 
                             RelativeSource={RelativeSource AncestorType=
                             {x:Type Button}}}" />                   
        </Button.Content>
</Button>

我试过了:

<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.Target="{Binding Path=Foreground, RelativeSource={RelativeSource TemplatedParent}}">
    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource LightForegroundBrush}" />
</ObjectAnimationUsingKeyFrames>

但是这会崩溃。 关于如何做到这一点的任何建议?提前致谢。

【问题讨论】:

    标签: wpf xaml data-binding


    【解决方案1】:

    我这边也有同样的问题....

    但也有可能是我们的思维方式不对。

    要设置样式的是“内容”,但没有人知道它会是什么内容。因此,“正确”的方式是说它是您放入内容中的控件的工作。

    例如,如果您将 Button 放入 ContentControl,则 Button 样式处理 VSM 而不是它周围的 ContentControl。

    这不是您问题的确切答案,但也许有助于理解为什么这不是某种选择。

    编辑: 经过一番研究,我发现了这一点:

    Animating a TextBox.Foreground in WPF

    感觉不是最佳做法,但它可能对您的问题有用。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-23
    • 2013-09-04
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    相关资源
    最近更新 更多