tags:

views:

350

answers:

3

Hi,

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.

+2  A: 

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

GWLlosa
Sure. That's what I had before, but I'm looking for a xaml-based solution if possible.
Steve the Plant
I think what I'm looking for is an EventSetter.
Steve the Plant
And it looks like EventSetter isn't supported by Trigger.
Steve the Plant
A: 

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.

mdm20
A: 

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.

Bryan Anderson