Adam
Adam

Reputation: 1223

Set Button Click handler with a DataTrigger

I'm trying to update some visual properties and the click handler on a button in response to the value of an enum. The visual properties are easy enough, but the click handler is giving me trouble.

The enum has 4 values: Launching, Launched, Terminating, Terminated. The button will launch or kill a process based on the current state. Here's what I have for the visual properties:

<Button>
    <Button.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ProcessState}" Value="{x:Static local:ProcessState.Launching}">
                    <Setter Property="IsEnabled" Value="false"/>
                    <Setter Property="Content" Value="Launching..."/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ProcessState}" Value="{x:Static local:ProcessState.Launched}">
                    <Setter Property="IsEnabled" Value="true"/>
                    <Setter Property="Content" Value="Terminate"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ProcessState}" Value="{x:Static local:ProcessState.Terminating}">
                    <Setter Property="IsEnabled" Value="false"/>
                    <Setter Property="Content" Value="Terminating..."/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ProcessState}" Value="{x:Static local:ProcessState.Terminated}">
                    <Setter Property="IsEnabled" Value="true"/>
                    <Setter Property="Content" Value="Launch"/>

                    <!-- Doesn't work!
                    <EventSetter Event="Click" Handler="LaunchSim_Click"/>
                    -->
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

The problem is that Style.Triggers doesn't support EventSetter so I can't update Button.Click with this approach. Now, I could using a single click handler and branch based on the enum, but I want to understand what is possible from the XAML side.

I also tried creating 4 separate styles in Button.Resources then swapping out the styles with DataTrigger but then you end up trying to change the button style using a style and I'm not convinced that works either.

Is there a reasonable way to change Button.Click from XAML in response to a changing data value?

Upvotes: 1

Views: 1886

Answers (2)

mm8
mm8

Reputation: 169160

Is there a reasonable way to change Button.Click from XAML in response to a changing data value?

No, there isn't. Using a single click handler and branch based on the enum sounds like a good idea. Alternatively, if you are familiar with the MVVM design pattern, you could bind to a command of a view model and handle your logic in there. It is indeed possible to set the Command property using a Setter in a Style, but remember that XAML is a markup language and just because you possible can do certain things in pure XAML, it doesn't mean that you always should.

Upvotes: 3

Bohdan Biehov
Bohdan Biehov

Reputation: 431

Instead of EventSetter you can do

<Setter Property="Command" Value="{Binding SomeCommand}"/>

Actually if you want a button to be clickable only in one state of enum you can just bind to "SomeCommand" and create a command which is enabled only when particular enum type is selected.

for example

<Button Command={Binding SomeCommand} >
   Style...
</Button>

In a view model you can do

SomeCommand = new RelayCommand(SomeMethod, ()=> ProcessState == ProcessState.Terminated);

Upvotes: 1

Related Questions