作为 StaticResource 的 XAML 触发器

发布于 2024-08-30 16:45:32 字数 790 浏览 3 评论 0原文

为什么我无法创建触发器并将其用作 XAML 中的静态资源?

<Window.Resources>
    <Trigger x:Key="ValidationTrigger" x:Shared="False" Property="Validation.HasError" Value="true">
        <Setter Property="FrameworkElement.ToolTip"
                Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                Path=(Validation.Errors)/ErrorContent}"/>
    </Trigger>

    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Style.Triggers>
            <StaticResource ResourceKey="ValidationTrigger"/>
        </Style.Triggers>
    </Style>
</Window.Resources>

我在运行时收到错误消息
“值不能为空。
参数名称:triggerBase 标记文件中的对象“System.Windows.Markup.StaticResourceHolder”发生错误”

Why can't I create a trigger and use it as a static resource in XAML?

<Window.Resources>
    <Trigger x:Key="ValidationTrigger" x:Shared="False" Property="Validation.HasError" Value="true">
        <Setter Property="FrameworkElement.ToolTip"
                Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                Path=(Validation.Errors)/ErrorContent}"/>
    </Trigger>

    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Style.Triggers>
            <StaticResource ResourceKey="ValidationTrigger"/>
        </Style.Triggers>
    </Style>
</Window.Resources>

I get an errormessage at runtime
"Value cannot be null.
Parameter name: triggerBase Error at object 'System.Windows.Markup.StaticResourceHolder' in markup file"

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

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

发布评论

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

评论(1

蹲墙角沉默 2024-09-06 16:45:32

我想这是因为资源首先被加载。

因此,在这种情况下,WPF 将尝试在可能与其关联的任何控件之前加载触发器。那么此时就无法评估触发值。

但这需要确认:)

I imagine that this is because resources are loaded first.

So in this case, WPF would try to load your trigger before any control it might be associated with. Then the trigger value can't be evaluated at that time.

But that need to be confirmed :)

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