WPF 触发器/样式覆盖另一个
我有一个 ListView
,其 ItemContainerStyle
定义如下:
<ListView Width="auto"
SelectionMode="Single"
ItemContainerStyle="{StaticResource ItemContStyle}"
....
然后在 baseListViewStyle
中,我定义了一些基本样式以应用于我的 ListView
s,包括一个Style
触发器:
<Style x:Key="baseListViewStyle" TargetType="ListViewItem">
<Setter Property="Height" Value="20" />
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
这里的Trigger
会在鼠标悬停在该行上时突出显示该行。好的。
我在 ListViewItem
上还有一个 DataTrigger
:
<Style.Triggers>
<DataTrigger Binding="{Binding IsTestTrue}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource SomeFunkyAnimation}" />
</DataTrigger.EnterActions>
</DataTrigger>
如果测试为真,则会播放一个漂亮的小淡入淡出动画。这一切都有效,除非我将鼠标移到“测试为真”的行上时动画停止并显示鼠标悬停样式。
有什么想法可以在我的 DataTrigger
中覆盖该样式吗?
TIA
更新:
SomeFunkyAnimation
对背景颜色进行动画处理。它的 xaml 位于此处:
<Style x:Key="ItemContStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource baseListViewStyle}">
<Style.Resources>
<Storyboard x:Key="SomeFunkyAnimation" FillBehavior="Stop">
<ColorAnimation Storyboard.TargetProperty="Background.Color" RepeatBehavior="Forever" From="Red" To="Pink" Duration="0:0:3"/>
</Storyboard>
</Style.Resources>
MouseOver
触发器在 baseListViewStyle
中定义。 DataTrigger
在ItemContStyle
中定义。
我尝试删除 MouseOver
样式触发器,但这不起作用,因为我相信 Listview
已经定义了默认的 MouseOver
样式,因此它会覆盖我的DataTrigger
动画。
I have a ListView
with an ItemContainerStyle
defined as such:
<ListView Width="auto"
SelectionMode="Single"
ItemContainerStyle="{StaticResource ItemContStyle}"
....
Then in baseListViewStyle
, I have defined some base styles to apply to my ListView
s, including a Style
trigger:
<Style x:Key="baseListViewStyle" TargetType="ListViewItem">
<Setter Property="Height" Value="20" />
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
The Trigger
here highlights the row when mouse is over it. Nice.
I also have a DataTrigger
on the ListViewItem
:
<Style.Triggers>
<DataTrigger Binding="{Binding IsTestTrue}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource SomeFunkyAnimation}" />
</DataTrigger.EnterActions>
</DataTrigger>
If test is true then a nice little fade animation is played out. This all works except when I move my mouse over the row where "test is true" the animation stops and the mouse over style is displayed.
Any ideas how I can override that style in my DataTrigger
?
TIA
Update:
SomeFunkyAnimation
animates the background colour. The xaml for it is here:
<Style x:Key="ItemContStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource baseListViewStyle}">
<Style.Resources>
<Storyboard x:Key="SomeFunkyAnimation" FillBehavior="Stop">
<ColorAnimation Storyboard.TargetProperty="Background.Color" RepeatBehavior="Forever" From="Red" To="Pink" Duration="0:0:3"/>
</Storyboard>
</Style.Resources>
The MouseOver
trigger is defined in baseListViewStyle
. The DataTrigger
is defined in ItemContStyle
.
I tried removing the MouseOver
style trigger but that didn't work as I believe the Listview
has a default MouseOver
style already defined so it overrides my DataTrigger
animation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在花了几个小时解决这个问题后,我终于找到了解决方法。由于某种原因,ColorAnimation 在鼠标悬停后停止动画。不知道为什么,也许是 wpf bug 谁知道。解决方案是重新调整我的动画。下面的代码做了同样的事情:
然后是相同的 DataTrigger,并带有一个额外的 Setter 作为背景:
最后和平了。
Well after dropping a few hours of my life on this problem I finally found a work around. For some reason ColorAnimation just stops animating after a mouse over. Dont know why, maybe a wpf bug who knows. Solution was to rejig my animation. The below does the same thing:
And then same DataTrigger with an additional Setter for the background:
Peace at last.