Benutzer:MovGP0/WPF/Triggers

aus Wikipedia, der freien Enzyklopädie
< Benutzer:MovGP0‎ | WPF
Dies ist die aktuelle Version dieser Seite, zuletzt bearbeitet am 28. Dezember 2015 um 15:07 Uhr durch imported>MovGP0(77247).
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


Triggers

  • change apperance or behavior when value changes or events are raised
  • must be a dependency property

PropertyTrigger

  • set property value or set action based on value
  • must contain setter objects
  • source property and value condition must be set
  • EnterActions and ExitActions
  • changes values back to original if condition is not met anymore
  • Event setter properties are not supported
<ResourceDictionary (...)>
    <Style TargetType="{x:Type local:MyControl}">
        <Setter Property="Background" Value="White">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MyControl}">
                    <Grid Background="{TemplateBinding Background}">
                        <!-- ... -->
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Green" />
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

MultiTrigger

  • listens to more than one property
<ResourceDictionary (...)>
    <Style TargetType="{x:Type local:MyControl}">
        <Setter Property="Background" Value="White">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MyControl}">
                    <Grid Background="{TemplateBinding Background}">
                        <TextBox x:Name="textBox" 
                                 Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}" />
                    </Grid>

                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="Text" Value="Hello" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" Value="Green" />
                            <Setter Property="BorderBrush" Value="Red" />
                            <Setter Property="BorderThickness" Value="5" TargetName="textBox" />
                        </MultiTrigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

EventTrigger

  • executes an Action on Event
  • Action will not be undone
  • only handeles events that are not handeled
<ResourceDictionary (...)>
    <Style TargetType="{x:Type local:MyControl}">
        <Setter Property="Background" Value="White">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MyControl}">
                    <Grid Background="{TemplateBinding Background}">
                        <TextBox x:Name="textBox" 
                                 Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}" />
                    </Grid>

                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="MouseEnter">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"
                                                    To="Green" Duration="0:0:0.2" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>

                        <EventTrigger RoutedEvent="MouseLeave">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"
                                                    To="White" Duration="0:0:0.2" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

DataTrigger

  • fires when the value of an data object changes

MultiDataTrigger