How do I use EventSetters with a Window Style?
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" Handl开发者_如何转开发er="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>
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.
精彩评论