Question

I'm trying to change a Button's Click property/event when a DataTrigger is triggered but I'm not sure if this is the best method to do it. In fact, it won't even compile :)

What I have looks like this:

<Style TargetType="{x:Type Button}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=ObjectTreeView, Path=SelectedItem.Replaceable}" Value="False">
            <Setter Property="Content" Value="Add" />
            <Setter Property="Button.Click" Value="AddObject_Click" />
        </DataTrigger>
        <DataTrigger Binding="{Binding ElementName=ObjectTreeView, Path=SelectedItem.Replaceable}" Value="True">
            <Setter Property="Content" Value="Replace" />
            <Setter Property="Button.Click" Value="ReplaceObject_Click" />
        </DataTrigger>
    </Style.Triggers>            
</Style>

Compiling gives me an error message saying "Cannot find the Style Property 'Click' on the type 'System.Windows.Controls.Button'"

Any suggestions? If this not possible, what alternatives are there?

Thanks!

Edit:

I thought I found the solution which was to use an EventSetter, but EventSetters aren't supported inside Triggers. What I thought would've worked was:

<EventSetter Event="Button.Click" Handlder="AddObject_Click" />

But like I said, this is supported at all.

Was it helpful?

Solution

Click isn't a property, it's an event. Buttons have a property IsPressed that becomes true when the button is being pressed. You could try using that.

OTHER TIPS

Wouldn't it be easier to just have one click event and in that event, an if statement based on your DataTrigger?

Instead of using a Click event, try using the button's Command property. You should be able to switch which command it points to based on a Trigger.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top