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?
The routed event then routes to successive parent elements, invoking their event handlers in turn, until it reaches the element tree root. Most routed events use the bubbling routing strategy.
The difference between the two, as the naming convention implies, is that a tunneling event will start at the highest node in the tree (probably the Window) and going down to the lowest child. A bubbling event will start at the child and then go upwards again.
Event setters invoke the specified event handlers in response to events.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With