首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从ControlTemplate中的MultiTrigger启动ColorAnimation?

如何从ControlTemplate中的MultiTrigger启动ColorAnimation?
EN

Stack Overflow用户
提问于 2010-03-16 22:34:33
回答 2查看 2.4K关注 0票数 0

我有以下WPF TabItem的ControlTemplate:

代码语言:javascript
复制
<ControlTemplate x:Key="DefaultTabItemTemplate" TargetType="{x:Type TabItem}">
    <ControlTemplate.Resources>
        <SolidColorBrush x:Key="UnselectedForegroundBrush" Color="#414141" />
        <!-- Unique color for this template -->
        <SolidColorBrush x:Key="SelectedForegroundBrush" Color="#457581" />
        <!-- Unique color for this template -->
        <SolidColorBrush x:Key="MouseOverTextBrush" x:Name="local_MouseOverTextBrush" Color="#FFF2F2F2"/>
    </ControlTemplate.Resources>
    <Grid>
        <Border Name="Border" MinHeight="30" Margin="0,0,0,-1" Background="{DynamicResource TabControlBackgroundBrush}" BorderBrush="{DynamicResource ndt_DisabledForegroundBrush}"  BorderThickness="1,1,1,1" CornerRadius="0,0,0,0" >
            <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" TextElement.FontStretch="UltraExpanded" TextElement.FontWeight="UltraBlack" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True" />
        </Border>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Panel.ZIndex" Value="2" />
            <Setter TargetName="Border" Property="Background" Value="{DynamicResource ndt_TabControlBackgroundBrush}" />
            <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
            <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{StaticResource SelectedForegroundBrush}" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="Border" Property="Background" Value="{DynamicResource ndt_DisabledBackgroundBrush}" />
            <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{DynamicResource ndt_DarkGray}" />
            <Setter Property="Foreground" Value="{DynamicResource ndt_DisabledForegroundBrush}" />
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsMouseOver" Value="True" />
                <Condition Property="IsSelected" Value="False" />
            </MultiTrigger.Conditions>
            <Setter TargetName="Border" Property="Background" Value="{DynamicResource ndt_NavigationAreaBrush}" />
            <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{DynamicResource MouseOverTextBrush}" />
        </MultiTrigger> 
    </ControlTemplate.Triggers>
</ControlTemplate>

到目前为止,一切运行正常。模板末尾的MultiTrigger为未选中的TabItems定义鼠标悬停效果。现在我认为这个鼠标悬停效果的颜色变化看起来有点轻率,所以让我们用一个ColorAnimation来动画它。但是不要在小鸡孵化之前就数鸡--我试过的所有东西都不起作用。也许我忽略了显而易见的事情--但如何实现这一壮举呢?

提前感谢

班仔

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-03-16 23:33:24

你试过MultiTrigger.EnterActions吗?

在您的MultiTrigger中,您将拥有类似以下内容的内容:

代码语言:javascript
复制
<MultiTrigger.EnterActions>
    <BeginStoryboard>
        <Storyboard>
            <ColorAnimation Storyboard.TargetName="YourObject'sName" Storyboard.TargetProperty="YourObject'sColorProperty" To="YourFavoriteColor" Duration"YourFavoriteNumber" />
        </Storyboard>
    </BeginStoryboard>
</MultiTrigger.EnterActions>

然后,如果您愿意,您可以随时添加以反转动画(或者当触发器不再为真时执行任何操作)。

希望这能有所帮助!

编辑:回答答案中提出的问题。我还没试过,但我不确定你能不能像那样直接把你的资源动画化。直接将资源设置为SolidColorBrush,而不是设置资源的背景:

代码语言:javascript
复制
<Border Name="Border" MinHeight="30" Margin="0,0,0,-1" BorderBrush="{DynamicResource ndt_DisabledForegroundBrush}"  BorderThickness="1,1,1,1" CornerRadius="0,0,0,0" > 
    <Border.Background>
        <SolidColorBrush x:Name="local_TabControlBackgroundBrush" Color="#CBCBCB" />
    </Border.Background>
    <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" TextElement.FontStretch="UltraExpanded" TextElement.FontWeight="UltraBlack" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True" /> 
</Border> 

那么你的动画就可以识别你的local_TabControlBackgroundBrush了!

此外,我认为您可能必须将MultiTrigger移到其他触发器的顶部。我认为当你的MultiTrigger为真时,你基于IsSelected的触发器也是真的,并将获得优先权,因为它被列在第一位。我可能是错的,但我会仔细检查,如果你没有收到错误,但你的多触发器仍然不起作用。

希望它能帮上忙!

票数 1
EN

Stack Overflow用户

发布于 2010-03-17 00:05:58

是的,我以前试过,但我在应用程序启动时遇到了运行时错误。不知道为什么,但现在我毫不费力地找到了原因。您不能像我第一次尝试时那样在ColorAnimation中使用动态资源。因此,以下模板在启动时不会产生运行时错误:

代码语言:javascript
复制
<ControlTemplate x:Key="DefaultTabItemTemplate" TargetType="{x:Type TabItem}">
    <ControlTemplate.Resources>
        <SolidColorBrush x:Key="UnselectedForegroundBrush" Color="#414141" />
        <!-- Unique color for this template -->
        <SolidColorBrush x:Key="SelectedForegroundBrush" Color="#457581" />
        <!-- Unique color for this template -->
        <SolidColorBrush x:Key="MouseOverTextBrush" x:Name="local_MouseOverTextBrush" Color="#FFF2F2F2"/>
        <!-- Unique color for this template -->
        <SolidColorBrush x:Key="TabControlBackgroundBrush" x:Name="local_TabControlBackgroundBrush" Color="#CBCBCB" />
    </ControlTemplate.Resources>
    <Grid>
        <Border Name="Border" MinHeight="30" Margin="0,0,0,-1" Background="{DynamicResource TabControlBackgroundBrush}" BorderBrush="{DynamicResource ndt_DisabledForegroundBrush}"  BorderThickness="1,1,1,1" CornerRadius="0,0,0,0" >
            <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" TextElement.FontStretch="UltraExpanded" TextElement.FontWeight="UltraBlack" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True" />
        </Border>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Panel.ZIndex" Value="2" />
            <Setter TargetName="Border" Property="Background" Value="{DynamicResource ndt_TabControlBackgroundBrush}" />
            <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
            <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{StaticResource SelectedForegroundBrush}" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="Border" Property="Background" Value="{DynamicResource ndt_DisabledBackgroundBrush}" />
            <Setter TargetName="ContentSite" Property="TextElement.Foreground" Value="{DynamicResource ndt_DarkGray}" />
            <Setter Property="Foreground" Value="{DynamicResource ndt_DisabledForegroundBrush}" />
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsMouseOver" Value="True" />
                <Condition Property="IsSelected" Value="False" />
            </MultiTrigger.Conditions>
            <MultiTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetName="local_TabControlBackgroundBrush" Storyboard.TargetProperty="Color" To="{StaticResource ndt_NavigationAreaColor}" Duration="0:0:0.15" />
                    </Storyboard>
                </BeginStoryboard>
            </MultiTrigger.EnterActions>
        </MultiTrigger> 
    </ControlTemplate.Triggers>
</ControlTemplate>

但是,遗憾的是,当MultiTrigger触发时,我得到一个类似于“无法在名称空间‘System.Windows.Control.ControlTemplate’中找到名称'local_TabControlBackgroundBrush‘”的错误消息。(我不是英语国家的人,所以直接引用错误信息没有什么实际意义。)该名称是在模板的资源中定义的-为什么他没有找到它?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2455189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档