作为 StaticResource 的 XAML 触发器
为什么我无法创建触发器并将其用作 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想这是因为资源首先被加载。
因此,在这种情况下,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 :)