La structure interne ainsi que le design d'AreaProg ont récemment été modifiés.
Suite à cela, le format de certains articles a été perturbé. Le problème est connu et en cours de résolution. Merci de votre compréhension.
<Window x:Class="coursWPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Cours WPF" Height="114" Width="400" Loaded="Window_Loaded">
<Window.Resources>
<Style x:Key="forExemple">
<Style.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Button.Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel x:Name="stack1">
<Button x:Name="bt1"
Content="Put your mouse over me!"
Style="{StaticResource forExemple}"
/>
</StackPanel>
</Window>
Comme on le voit ici, le trigger est placé dans un style et englobe un objet Setter. Les différentes propriétés à initialiser pour un trigger sont :
<Window x:Class="coursWPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Cours WPF" Height="114" Width="400" Loaded="Window_Loaded"
>
<Window.Resources>
<Style x:Key="forExemple">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=enter , Path=Text}" Value="trigger">
<Setter Property="Label.Background" Value="Red"/>
<Setter Property="Label.Foreground" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel x:Name="stack1">
<TextBox x:Name="enter"
Width="200"
Height="25"
/>
<Label x:Name="label"
Content="Watch this!"
Style="{StaticResource forExemple}"
/>
</StackPanel>
</Window>
Voici le résultat :
Explications :
L’implémentation du trigger reste la même, il est toujours appliqué à un style. Cependant, nous n’utilisons plus la classe Trigger mais bien DataTrigger. Quelles sont les nouvelles propriétés intéressantes de cette classe :
Binding : Nous n’avons pas encore vu le binding au niveau de WPF, retenez juste que cette propriété sert à lier la propriété d’un autre objet à surveiller, à notre trigger.
<Window x:Class="coursWPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Cours WPF" Height="114" Width="400" Loaded="Window_Loaded"
>
<Window.Resources>
<Style x:Key="forExemple">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Button.IsMouseOver" Value="True"/>
<Condition Property="Button.IsFocused" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Button.Background" Value="Red"/>
</MultiTrigger.Setters>
</MultiTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel x:Name="stack1">
<Button x:Name="bt1"
Content="Test me!"
Style="{StaticResource forExemple}"
/>
</StackPanel>
</Window>
On affecte à la propriété Triggers de l’objet Style un objet Multitrigger. Cet objet a une propriété Conditions qui va contenir en fait une collection d’objet Condition , et c’est les propriétés de ces objets (Property et Value) qu’il faut initialiser.
<Window x:Class="coursWPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Cours WPF" Height="114" Width="400" Loaded="Window_Loaded"
>
<Window.Resources>
<Style x:Key="forExemple">
<Style.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<SoundPlayerAction Source="C:Ding.wav" />
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel x:Name="stack1">
<Button x:Name="bt1"
Content="Test me!"
Style="{StaticResource forExemple}"
/>
</StackPanel>
</Window>
Nous voyons bien ici que le trigger n’est plus lié à une propriété mais bien à un évènement (ici l’évènement Click d’un Button) et que les actions à éffectuer se situent bien dans une collection d’Actions. Veuillez vous identifier ou vous inscrire pour donner une note à cet article.
Veuillez vous identifier ou vous inscrire pour réagir à cet article.
Istace Emmanuel (02/05/2011 - 20:57)
orel (14/05/2012 - 12:01)
La il n'y a qu'un exemple avec SoundPlayerAction.
Merci.
sofi@ne (28/04/2013 - 15:03)