如何将 EventSetters 与窗口样式一起使用?
我设计了一个窗口来替换标准 Chrome,并且我想使用 EventSetters 处理激活和停用事件。我收到错误“...'Activated'必须是已注册的 RoutedEvent...”:
<EventSetter Event="Activated" Handler="Window_Activated"/>
但是,这在相同的风格下工作得很好。
<EventSetter Event="Loaded" Handler="Window_Loaded"/>
有人遇到过这个或者知道怎么回事吗?
编辑:
<Style x:Key="Window_Cartesia" TargetType="{x:Type Window}">
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="Background" Value="Transparent"/>
<EventSetter Event="Loaded" Handler="Loaded"/>
<EventSetter Event="Activated" Handler="Window_Activated"/>
<EventSetter Event="KeyDown" Handler="KeyDown"/>
...
编辑:
这似乎涵盖了它。
在 Loaded 事件中定义:
AddHandler Win.Activated, AddressOf Activated
AddHandler Win.Deactivated, AddressOf Deactivated
因为这是样式的代码隐藏,所以我需要一个实例引用,即 Win。我不知道这是否是完成此操作的最佳方法,但是...
编辑 1:
或者,IsActive 的触发器可以在 xaml 中处理它。
<Trigger Property="IsActive" Value="True">
...
</Trigger>
I have styled a window to replace the standard Chrome and I want to handle the Activated and Deactivated events using EventSetters. I get an error "...'Activated' must be a RoutedEvent registered..." with this:
<EventSetter Event="Activated" Handler="Window_Activated"/>
However, this works fine in the same style.
<EventSetter Event="Loaded" Handler="Window_Loaded"/>
Anyone run across this or know what's up?
Edit:
<Style x:Key="Window_Cartesia" TargetType="{x:Type Window}">
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="Background" Value="Transparent"/>
<EventSetter Event="Loaded" Handler="Loaded"/>
<EventSetter Event="Activated" Handler="Window_Activated"/>
<EventSetter Event="KeyDown" Handler="KeyDown"/>
...
EDIT:
This seems to cover it.
Defined in the Loaded event:
AddHandler Win.Activated, AddressOf Activated
AddHandler Win.Deactivated, AddressOf Deactivated
Because this is code behind for a style, I need an instance reference which is Win. I don't know if this is the best way to accomplish this but...
EDIT 1:
Alternatively, a trigger for IsActive to handle it in xaml.
<Trigger Property="IsActive" Value="True">
...
</Trigger>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能粘贴完整的样式声明吗?样式的目标类型可能存在问题。 Loaded 在FrameworkElement 上定义,而Activated 在Window 上定义。尝试将 TargetType={x:Type Window} 设置为样式元素中的属性。
编辑:激活不是路由事件。因此不可能按照您的风格使用它。或者,您可以在代码隐藏中订阅此事件。
Could you paste your complete style declaration? There might be an issue with the style's target type. Loaded is defined on a FrameworkElement whilst Activated is defined on a Window. Try setting TargetType={x:Type Window} as an attribute in the style element.
Edit: Activated is not a routed event. Therefore it is not possible to use it in your style. Alternatively, you could subscribe to this event in code behind.