Marcus
Marcus

Reputation: 5447

EventSetter for non-routed event?

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?

Upvotes: 8

Views: 4101

Answers (2)

MaRuf
MaRuf

Reputation: 1894

Like H.B. said you can't use EventSetter in a Triggers collection, you should check in the event handler if it is logged or not and execute the right code, doesn't this solution work for you?

Upvotes: 0

brunnerh
brunnerh

Reputation: 184526

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.)

Upvotes: 7

Related Questions