c# - ControlTemplate Storyboard 颜色动画问题

标签 c# .net wpf xaml animation

我对彩色动画有疑问。这是我的来源:

 <Window.Resources>
    <hedit:BrushToColorConverter x:Key="BrushToColorConverter" />
    <Style x:Key="MyButtonStyle" TargetType="Button">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="buttonAnimIn">
                            <!-- Problem line -->
                            <ColorAnimation Storyboard.TargetName="bntBack" Storyboard.TargetProperty="Color" To="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Converter={StaticResource BrushToColorConverter}}" />
                        </Storyboard>
                        <Storyboard x:Key="buttonAnimOut">
                            <ColorAnimation Storyboard.TargetName="bntBack" Storyboard.TargetProperty="Color" To="Blue" />
                        </Storyboard>
                        <Storyboard x:Key="buttonAnimForegroundIn">
                            <ColorAnimation Storyboard.TargetName="btnFore" Storyboard.TargetProperty="Color" To="Blue" />
                        </Storyboard>
                        <Storyboard x:Key="buttonAnimForegroundOut">
                            <ColorAnimation Storyboard.TargetName="btnFore" Storyboard.TargetProperty="Color" To="Red" />
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <Border Name="border" 
                        BorderThickness="1"
                        Padding="4,2" 
                        BorderBrush="DarkGray" 
                        CornerRadius="3">
                        <Border.Background>
                            <SolidColorBrush Color="Blue" x:Name="bntBack" />
                        </Border.Background>
                        <ContentControl HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding Content}">
                            <ContentControl.Foreground>
                                <SolidColorBrush Color="Red" x:Name="btnFore" />
                            </ContentControl.Foreground>
                        </ContentControl >
                    </Border>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="Button.MouseEnter">
                            <BeginStoryboard Storyboard="{StaticResource buttonAnimIn}" />
                            <BeginStoryboard Storyboard="{StaticResource buttonAnimForegroundIn}" />
                        </EventTrigger>
                        <EventTrigger RoutedEvent="Button.MouseLeave">
                            <BeginStoryboard Storyboard="{StaticResource buttonAnimOut}" />
                            <BeginStoryboard Storyboard="{StaticResource buttonAnimForegroundOut}" />
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

问题是:

无法将“Style”属性中的值转换为“System.Windows.Style”类型的对象。无法卡住此 Storyboard时间线树以供跨线程使用。标记文件“HLSLEditor;component/mainwindow.xaml”第 223 行位置 25 中的对象“System.Windows.Controls.Button”出错。

当使用固定颜色时它可以工作,但它不能与父级的前景色一起工作...

如何为前景色或背景色制作动画?

谢谢!

最佳答案

您不能卡住绑定(bind),您可以通过将颜色声明为资源然后在动画中使用 StaticResource 时将控件的背景绑定(bind)到它来解决这个问题。

例如

<Window.Background>
    <SolidColorBrush Color="{DynamicResource Background}"/>
</Window.Background>
<Window.Resources>
    <Color x:Key="Background">Green</Color>
</Window.Resources>
<ColorAnimation Storyboard.TargetProperty="Foreground.Color"
                Duration="0:0:1"
                To="{StaticResource Background}"/>

使用资源类的替代方法:

public static class MyColors
{
    public static Color MyHighlightColor = Color.FromArgb(255, 0, 88, 0);
}
<ColorAnimation Storyboard.TargetProperty="Foreground.Color"
                Duration="0:0:1"
                To="{x:Static local:MyColors.MyHighlightColor}"/>

关于c# - ControlTemplate Storyboard 颜色动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5667987/

相关文章:

c# - 为什么不调用 AsyncTask 中的 OnProgressUpdate?

.net - mscordacwks.dll 和 mscorwks.dll 混淆

.net - 我应该使用 TDD 吗?

c# - WPF 图表工具包 : How to set a fixed X-axis range while adding real time data

c# - 在 FlowDocument 中启用嵌入式控件

c# - 在 WinRT 中,静态字段根本没有初始化

c# - 清除 TreeView

c# - 从图像生成视频并添加音频时,视频无法播放

c# - 页面级别的 ASP.NET 和 IsNew

c# - WPF 调度程序 {"The calling thread cannot access this object because a different thread owns it."}