Ugrás a tartalomhoz

.NET Programming Technologies

Gergely Kovásznai, Csaba Biró

Eszterházy Károly College

10. fejezet - Triggers (written by Csaba Biró)

10. fejezet - Triggers (written by Csaba Biró)

Triggers often be assigned to styles. We can set how a control reacts to the occurring of a particular event, or to the change of a feature. They provide supports for Style, ControlTemplate, DataTemplate (Chapter XIV) and FrameworkElement classes.

There are five different trigger, they are the following:

  1. Trigger

  2. DataTrigger

  3. EventTrigger

  4. MultiTrigger

  5. MultiDataTrigger

Trigger

This is the simplest and the most frequently used ’trigger’ is activated (validates the changes assigned to the controller) when the feature of the trigger fills any requirements.

Example X.1 Property Trigger 1

In the following example, we produced a trigger to the style can be assigned for buttons, which monitors the IsPressed property, and when the condition becomes true, a bond font style will be set on the text of the pushbutton.

X.1. Property trigger 1

<Grid>

    <Grid.Resources>

            <Style x:Key="Trigger" TargetType="Button">

                <Style.Triggers>

                    <Trigger Property="IsPressed" Value="True">

                        <Setter Property="FontWeight" Value="Bold"/>

                    </Trigger>

                </Style.Triggers>

            </Style>

    </Grid.Resources>

<Button  FontSize="30" Height="50" Width="250"

         Style="{StaticResource  ResourceKey=Trigger}">

         Property trigger

</Button>

</Grid>

Example X.2 Property Trigger 2

X.2. Property trigger 2

<Grid.Resources>

            <Style x:Key="Trigger2">

                <Style.Triggers>

                    <Trigger Property="Label.IsMouseOver" Value="True">

                        <Setter Property="Label.FontWeight" Value="Bold"/>

                        <Setter Property="Label.Foreground" Value="Red" />

                    </Trigger>            

                </Style.Triggers>

            </Style>

        </Grid.Resources>

<Label FontSize="38" Style="{StaticResource ResourceKey=Trigger2}">Property Trigger 2</Label>

</Grid>