WPF 触发器和样式

发布于 2024-09-04 00:07:20 字数 1112 浏览 1 评论 0原文

我有一个 UserControl,在资源部分我有这样的代码,

    <Style TargetType="{x:Type Viewbox}">

        <Style.Triggers>
            <Trigger Property="Viewbox.IsMouseOver" Value="True">
                <Setter Property="Viewbox.Effect" >
                    <Setter.Value>
                        <DropShadowEffect Color="DarkGray" Direction="45" BlurRadius="30"></DropShadowEffect>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

这适用于我在此控件中拥有的 ViewBox,

但是当我

<Viewbox.Triggers>
        <Trigger Property="Viewbox.IsMouseOver" Value="True">
            <Setter Property="Viewbox.Effect" >
                <Setter.Value>
                    <DropShadowEffect></DropShadowEffect>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Viewbox.Triggers>

在 View Box 中执行此操作时,我会收到错误......

有没有办法将本地触发器添加到ViewBox 不使用样式?

I have a UserControl, in the Resources section I have code like

    <Style TargetType="{x:Type Viewbox}">

        <Style.Triggers>
            <Trigger Property="Viewbox.IsMouseOver" Value="True">
                <Setter Property="Viewbox.Effect" >
                    <Setter.Value>
                        <DropShadowEffect Color="DarkGray" Direction="45" BlurRadius="30"></DropShadowEffect>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

SO this works for the ViewBox that I have in this control,

but when I do

<Viewbox.Triggers>
        <Trigger Property="Viewbox.IsMouseOver" Value="True">
            <Setter Property="Viewbox.Effect" >
                <Setter.Value>
                    <DropShadowEffect></DropShadowEffect>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Viewbox.Triggers>

within the View Box I get errors....

Is there a way to add local triggers to the ViewBox without using a Style?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

疧_╮線 2024-09-11 00:07:20

来自 MSDN

触发器集合
仅建立在一个元素上
支持EventTrigger,不支持属性
触发器(触发器)。如果您需要
属性触发器,您必须放置
这些在样式或模板中,并且
然后将该样式或模板分配给
该元素可以直接通过
Style 属性,或间接
通过隐式样式引用。

From MSDN

The collection of triggers
established on an element only
supports EventTrigger, not property
triggers (Trigger). If you require
property triggers, you must place
these within a style or template and
then assign that style or template to
the element either directly through
the Style property, or indirectly
through an implicit style reference.

谈下烟灰 2024-09-11 00:07:20

FrameworkElements 与样式和模板的触发器集合接受不同类型的触发器。对于 FrameworkElement.Triggers,您只能使用 EventTrigger,通常用于启动动画。 Style、ControlTemplate 和 DataTemplate 触发器集合可以使用您正在使用的更常见的触发器和数据触发器。这是一个奇怪的设置,总是让人感觉像是一个未完成的功能。您可以更改代码以使用 Storyboard 从 EventTrigger 为 DropShadowEffect 上的属性设置动画,或者仅使用 Style 方法。

The Triggers collections of FrameworkElements vs Styles and Templates accept different types of triggers. For FrameworkElement.Triggers you can only use EventTrigger, commonly used to start animations. Style, ControlTemplate, and DataTemplate Triggers collections can use the more common Trigger and DataTrigger like you're using. It's an odd setup that's always sort of felt like an unfinished feature. You could change your code to use a Storyboard to animate properties on the DropShadowEffect from an EventTrigger or just use the Style approach.

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