views:

46

answers:

2

Hi,

I get the following error on the WPF code bellow: 'Setter' object cannot be added to 'EventTrigger'. The given object must be an instance of TriggerAction or a derived type.

<Style x:Key="LinkLabel" TargetType="{x:Type Label}">

    <Setter Property="FontFamily" Value="Tahoma"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="Foreground" Value="DarkBlue"/>

    <Style.Triggers>

        <EventTrigger RoutedEvent="MouseEnter" >
            <Setter Property="Cursor" Value="Hand"/>
        </EventTrigger>

        <EventTrigger RoutedEvent="MouseDown" >
            <Setter Property="Foreground" Value="Red"/>
        </EventTrigger>

         <EventTrigger RoutedEvent="MouseUp" >
            <Setter Property="Foreground" Value="DarkBlue"/>
        </EventTrigger>

    </Style.Triggers>

</Style>

Can anyone explain and point me in the right direction ?

MadSeb

A: 

You must use a TriggerAction to change the values as in this example from the EventTriggers MSDN page:

<Style TargetType="Rectangle">
  <Setter Property="Width" Value="50" />
  <Setter Property="Height" Value="50" />
  <Setter Property="Margin" Value="20" />
  <Setter Property="HorizontalAlignment" Value="Left" />
  <Style.Triggers>
    <EventTrigger RoutedEvent="MouseEnter">
        <BeginStoryboard>
            <Storyboard>
              <DoubleAnimation To="300" Duration="0:0:1.5" 
                AccelerationRatio="0.10" DecelerationRatio="0.25" 
                Storyboard.TargetProperty="(Canvas.Width)" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
    <EventTrigger RoutedEvent="MouseLeave">
        <BeginStoryboard>
            <Storyboard>
              <DoubleAnimation Duration="0:0:1.5" 
                AccelerationRatio="0.10" DecelerationRatio="0.25" 
                Storyboard.TargetProperty="(Canvas.Width)" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
  </Style.Triggers>
</Style>
ChrisF
+2  A: 

Option 1 - Use BeginStoryboard like ChrisF said

Option 2 - Don't use EventTrigger - for example, for a label that changes background color when you mouse over it use:

<Style TargetType="Label">
    <Setter Property="Background" Value="Blue"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true" >
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>

Unfortunately there is no property IsMouseDown you can use.

About your example:

  1. You don't have to set the cursor on MouseEnter, just set the Cursor property and it will only affect the mouse cursor when the mouse is over the control.

  2. If you want to create an hyperlink control don't use a label, eitehr use a Button with a custom template (this will give you the Click event and IsPressed property) or, even better - use the Hyperlink class like this:

    <TextBlock><Hyperlink>This is a link</Hyperlink></TextBlock>
    

This will have all the styling you wanted.

Nir