wpf - 并非所有 StackPanel 的 DataTriggers 都在工作
问题描述
我有一个显示文本和图像的按钮:为此,我使用带有 TextBlock 和 Image 的 StackPanel。
当变量“ActiveState”改变时,StackPanel 的背景也应该改变,为此我使用 DataTriggers
ActiveState=0 -> Red / ActiveState=1 -> Blue / ActiveState=2 -> 闪烁的蓝色(为此我使用情节提要和彩色动画)
闪烁的触发器 (Value=2) 工作正常,但其他两个触发器 (Value=0 + Value=1) 不工作。
当我删除 Stackpanel 的背景 (Background="Transparent") 时,前两个触发器正在工作,但最后一个触发器出现以下异常:
PresentationFramework.dll 中出现“System.InvalidOperationException”类型的未处理异常
附加信息:背景属性不指向路径“(0).(1)”中的依赖对象
这是我的代码:
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<StackPanel Orientation="Horizontal" Name="SelectButtonStackpanel" Background="Transparent">
<TextBlock Text="{Binding Text}"/>
<Image Source="{Binding Image}" Stretch="Uniform" Height="40" Width="40"/>
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Style.Triggers>
<DataTrigger Binding="{Binding ActiveState}" Value="0">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding ActiveState}" Value="1">
<Setter Property="Background" Value="Blue"/>
</DataTrigger>
<DataTrigger Binding="{Binding ActiveState}" Value="2">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="(StackPanel.Background).(SolidColorBrush.Color)"
To="Blue" Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever"
>
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="(StackPanel.Background).(SolidColorBrush.Color)"
Duration="0:0:1"
>
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
你知道我是如何让所有三个触发器工作的吗?
最好的问候菲尔
解决方案
当您直接Background="Transparent"
在 StackPanel 上设置时,它的优先级高于从样式设置器中设置的值。所以删除直接分配并为默认背景添加另一个 Setter。
除此之外,如果您想制作动画Background.Color
,您应该始终明确指定 SolidColorBrushes 而不是预定义的 Brushes,如Background="Red"
.
<StackPanel Orientation="Horizontal" Name="SelectButtonStackpanel">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Transparent"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding ActiveState}" Value="0">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Red"/>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ActiveState}" Value="1">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Blue"/>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ActiveState}" Value="2">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="Background.Color"
To="Blue" Duration="0:0:1"
AutoReverse="True" RepeatBehavior="Forever">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
</StackPanel>
推荐阅读
- jquery - Pre 在模态的选择框中选择一个选项,具体取决于另一个页面中的其他选定值
- elasticsearch - Envoy 代理日志的 Grok 模式
- r - 奇怪的错误:在ggplot中找不到对象'x'
- matplotlib - 在一张图中绘制各种图
- reactjs - 如何使用 Apollo graphql 实现 react-sortable-hoc?
- mysql - 在 MySQL 中获取组合结果
- python - 无法在 python 中将字符串转换为日期时间对象并使用它执行一些与时间相关的计算
- javascript - 如何在 React 和 Typescript 中将组件作为道具传递?
- react-native - 反应原生 useState 钩子不使用自定义钩子中的 axios 通过异步调用更新状态
- node.js - 添加节点 NAPI 函数作为回调到本机库