非路由事件的 EventSetter?

发布于 2024-12-12 21:58:11 字数 1248 浏览 0 评论 0原文

我有一个 Label,我正在尝试使用 Trigger 更改用户是否登录的 MouseDown 事件。

<Label x:Name="lblSave" MouseDown="lblSave_MouseDown" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" ToolTip="Save Files"  Width="25" Height="25" Margin="0, 0, 5, 0"  >
<Label.Style>
    <Style TargetType="{x:Type Label}">
        <Setter Property="Background" Value="{DynamicResource ResourceKey=saveIcon}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding Source={x:Static sec:Security.Instance}, Path=IsLoggedIn}" Value="False">
                <Setter Property="Background" Value="{DynamicResource ResourceKey=readonlyIcon}" />
                <EventSetter Event="MouseDown" Handler="lblNoAccess_MouseDown" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Label.Style>

但是,与这篇文章相反,我所拥有的不起作用,因为 MouseDown 不是路由事件。我仍然收到 System.Windows.EventSetter.Event 错误:{"Value can not be null.\r\nParameter name: value"}

所以我的问题是,有没有办法使用触发器来设置非路由事件?

I have a Label that I'm trying to change the MouseDown event for if a user is logged in or not by using a Trigger.

<Label x:Name="lblSave" MouseDown="lblSave_MouseDown" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" ToolTip="Save Files"  Width="25" Height="25" Margin="0, 0, 5, 0"  >
<Label.Style>
    <Style TargetType="{x:Type Label}">
        <Setter Property="Background" Value="{DynamicResource ResourceKey=saveIcon}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding Source={x:Static sec:Security.Instance}, Path=IsLoggedIn}" Value="False">
                <Setter Property="Background" Value="{DynamicResource ResourceKey=readonlyIcon}" />
                <EventSetter Event="MouseDown" Handler="lblNoAccess_MouseDown" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Label.Style>

But, contrary to this post, what I have won't work because MouseDown is not a routed event. I'm still getting the System.Windows.EventSetter.Event error: {"Value cannot be null.\r\nParameter name: value"}.

So my question is, is there a way to use a trigger to set non-routed events?

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

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

发布评论

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

评论(2

风苍溪 2024-12-19 21:58:12

就像 HB 所说的那样,您不能在 Triggers 集合中使用 EventSetter,您应该检查事件处理程序是否已记录并执行正确的代码,这个解决方案不适合您吗?

Like H.B. said you can't use EventSetter in a Triggers collection, you should check in the event handler if it is logged or not and execute the right code, doesn't this solution work for you?

极度宠爱 2024-12-19 21:58:11

来自文档

请注意,只有 Style.Setter 支持 EventSetter 对象。触发器(TriggerBase 和派生类)不支持 EventSetter。

(也如评论中所述,MouseDown 已路由。)

From the documentation:

Note that only Style.Setters supports EventSetter objects. Triggers (TriggerBase and derived classes) do not support EventSetter.

(Also as noted in the comment, MouseDown is routed.)

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