WPF 触发器/样式覆盖另一个

发布于 2024-09-02 09:09:14 字数 2388 浏览 2 评论 0原文

我有一个 ListView ,其 ItemContainerStyle 定义如下:

                            <ListView Width="auto" 
                              SelectionMode="Single"
                              ItemContainerStyle="{StaticResource ItemContStyle}"
                               .... 

然后在 baseListViewStyle 中,我定义了一些基本样式以应用于我的 ListViews,包括一个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 中定义。 DataTriggerItemContStyle 中定义。

我尝试删除 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 ListViews, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓 2024-09-09 09:09:14

在花了几个小时解决这个问题后,我终于找到了解决方法。由于某种原因,ColorAnimation 在鼠标悬停后停止动画。不知道为什么,也许是 wpf bug 谁知道。解决方案是重新调整我的动画。下面的代码做了同样的事情:

                    <Storyboard x:Key="SomeFunkyAnimation" FillBehavior="Stop">
                    <DoubleAnimation Storyboard.TargetProperty="Background.Opacity" RepeatBehavior="Forever" AutoReverse="true"  From="0.2" To="1.0" Duration="0:0:1"/>
                </Storyboard>

然后是相同的 DataTrigger,并带有一个额外的 Setter 作为背景:

                <DataTrigger Binding="{Binding IsTestTrue}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource SomeFunkyAnimation}" />
                    </DataTrigger.EnterActions>
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>

最后和平了。

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:

                    <Storyboard x:Key="SomeFunkyAnimation" FillBehavior="Stop">
                    <DoubleAnimation Storyboard.TargetProperty="Background.Opacity" RepeatBehavior="Forever" AutoReverse="true"  From="0.2" To="1.0" Duration="0:0:1"/>
                </Storyboard>

And then same DataTrigger with an additional Setter for the background:

                <DataTrigger Binding="{Binding IsTestTrue}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource SomeFunkyAnimation}" />
                    </DataTrigger.EnterActions>
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>

Peace at last.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文