Dan
Dan

Reputation: 23

Adding my own WPF TriggerAction

I've been trying to add a custom TriggerAction to our code. The following is the XAML snippet:

<Style x:Key="ToolBarButtonStyle" TargetType="Button">
    <Style.Triggers>
        <EventTrigger RoutedEvent="PreviewMouseDown">
            <actions:MyAction />
        </EventTrigger>
    </Style.Triggers>
    ...

MyAction is defined as follows:

  public class MyAction: TriggerAction<DependencyObject>
  {
    protected override void Invoke(object parameter)
    {
        // Do something
    }
  }

However, when I run the code, I get the following exception:

1) The given object must be an instance of TriggerAction or a derived type.

Resulting in: 'Add value to collection of type 'System.Windows.TriggerActionCollection' threw an exception.' Line number '29' and line position '15'.

I am running Visual Studio 2010, targeting .Net 4.0. Any suggestions?

Upvotes: 2

Views: 4747

Answers (1)

DareToExplore
DareToExplore

Reputation: 259

Adding any TriggerAction under the 'Event Trigger' of Styles expects the action to be of type

System.Windows.TriggerAction

Your custom action 'My Action' uses the

System.Windows.Interactivity.TriggerAction<T> (Generic Type)

and hence this error. You can try the following. This seems to be working in your case.

    <Button Content="Custom Action" Width="150" Height="50">
        <interact:Interaction.Triggers>
            <interact:EventTrigger EventName="PreviewMouseDown">
                <actions:MyAction></actions:MyAction>
            </interact:EventTrigger>
        </interact:Interaction.Triggers>            
    </Button>

Here, interact is the alias added for Interactivity. Let me know in case you need any more information.

Upvotes: 3

Related Questions