Wpf 如何从ControlTemplate中的MultiTrigger启动彩色动画?
我有以下WPF选项卡项的ControlTemplate:Wpf 如何从ControlTemplate中的MultiTrigger启动彩色动画?,wpf,xaml,controltemplate,Wpf,Xaml,Controltemplate,我有以下WPF选项卡项的ControlTemplate: <ControlTemplate x:Key="DefaultTabItemTemplate" TargetType="{x:Type TabItem}"> <ControlTemplate.Resources> <SolidColorBrush x:Key="UnselectedForegroundBrush" Color="#414141" /> <!-
<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为未选中的选项卡项定义鼠标悬停效果。
现在我认为鼠标悬停效果的颜色变化看起来有点鲁莽,所以让我们用彩色动画来制作动画。但是不要在小鸡孵化之前数它们——我尝试过的一切都不起作用。
也许我监督着显而易见的事情,但如何实现这一壮举呢
提前谢谢
班扎伊你试过MultiTrigger.entreactions吗 在您的MultiTrigger中,您将有如下内容:
<MultiTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="YourObject'sName" Storyboard.TargetProperty="YourObject'sColorProperty" To="YourFavoriteColor" Duration"YourFavoriteNumber" />
</Storyboard>
</BeginStoryboard>
</MultiTrigger.EnterActions>
然后,如果愿意,可以随时添加以反转动画(或者在触发器不再为真时执行任何操作)
希望这有帮助
编辑:
回答你在回答中提出的问题。
我还没有试过,但我不确定你是否可以像那样直接为你的资源设置动画。与其将背景设置为资源,不如直接将其设置为SolidColorBrush:
<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>
然后,您的动画可能能够识别您的本地\u TabControlBackgroundBrush
此外,我认为你可能必须将你的多触发器移动到高于其他触发器的顶部。我认为当你的多触发器为真时,你基于IsSelected的触发器也是真的,并且会得到优先权,因为它列在第一位。我可能是错的,但我会再次检查,如果你没有得到错误,但你的多触发器继续不工作
希望有帮助 是的,我以前尝试过,但在应用程序启动时遇到运行时错误。不知道为什么,但现在我毫无疑问地找到了原因。您不允许在彩色动画中使用动态资源,就像我第一次尝试时所做的那样。因此,以下模板在启动时不会产生运行时错误:
<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'”(我不是英语国家的人,因此直接引用错误消息没有实际用途。)
该名称在模板的参考资料中定义-他为什么没有找到它?与其将其作为答案发布,您可能希望只编辑您的问题。我已经编辑了我的答案,包括对你后续问题的回答。效果很好-非常感谢!!!我只是回答了我自己的问题,以便有机会发布格式化代码。没有想到要修改我原来的问题-对不起。