none
Appliquer un Style à tous les Labels d'une solution RRS feed

  • Question

  • Bonjour, voici un code qui me permet d'appliquer un style de couleur à un label ciblé dans un Window.

    Je souhaiterais pouvoir utiliser ce code pour tous les labels de la solution. Comment le transféré dans Application.xaml / Application.resources ?

       <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:YouTubeTest"
            xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" x:Class="MainWindow"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525" Background="#FF252526">
        <Window.Resources>
          
      
        </Window.Resources>
        <Grid>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="VisualStateGroup">
                    <VisualStateGroup.Transitions>
                        <VisualTransition GeneratedDuration="0:0:1">
                            <VisualTransition.GeneratedEasingFunction>
                                <ExponentialEase EasingMode="EaseIn"/>
                            </VisualTransition.GeneratedEasingFunction>
                        </VisualTransition>
                        <VisualTransition From="LabelRed" GeneratedDuration="0:0:1" To="LabelGreen">
                            <VisualTransition.GeneratedEasingFunction>
                                <ExponentialEase EasingMode="EaseIn"/>
                            </VisualTransition.GeneratedEasingFunction>
                        </VisualTransition>
                    </VisualStateGroup.Transitions>
                    <VisualState x:Name="LabelRed">
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" 
                                                          Storyboard.TargetName="label">
                                <EasingColorKeyFrame KeyTime="0" Value="#FFD81313"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="LabelGreen">
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" 
                                                          Storyboard.TargetName="label">
                                <EasingColorKeyFrame KeyTime="0" Value="#FF1D9E1D"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
    
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Label x:Name="label" Content="Label" Margin="34,10,50.5,10">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseEnter">
                        <ei:GoToStateAction StateName="LabelRed"/>
                    </i:EventTrigger>
                    <i:EventTrigger EventName="MouseLeave">
                        <ei:GoToStateAction StateName="LabelGreen"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Label>
    
        </Grid>
    </Window>


    JF Collombet ® CreateSpecificCulture

    jeudi 11 août 2016 05:13

Réponses

  • J'ai trouver la solution voici le code si cela peut aider d'autre personnes :

        <Window.Resources>
    
            <Style x:Key="LabelStyle1" TargetType="{x:Type Label}">
                <Setter Property="Foreground" Value="AliceBlue"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="Padding" Value="5"/>
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="FontSize" Value="20"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Label}">
                            <Border BorderBrush="{TemplateBinding BorderBrush}" 
                                    BorderThickness="{TemplateBinding BorderThickness}" 
                                    Background="{TemplateBinding Background}" 
                                    Padding="{TemplateBinding Padding}"
                                    SnapsToDevicePixels="true">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="VisualStateGroup">
                                        <VisualStateGroup.Transitions>
                                            <VisualTransition From="LabelGreen" GeneratedDuration="0:0:1" To="LabelAlice">
                                                <VisualTransition.GeneratedEasingFunction>
                                                    <ExponentialEase EasingMode="EaseIn"/>
                                                </VisualTransition.GeneratedEasingFunction>
                                            </VisualTransition>
                                        </VisualStateGroup.Transitions>
    
                                        <VisualState x:Name="LabelGreen">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground)" Storyboard.TargetName="contentPresenter">
                                                    <DiscreteObjectKeyFrame KeyTime="0">
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <SolidColorBrush Color="Green"/>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="LabelAlice">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground)" Storyboard.TargetName="contentPresenter">
                                                    <DiscreteObjectKeyFrame KeyTime="0">
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <SolidColorBrush Color="AliceBlue"/>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <ContentPresenter x:Name="contentPresenter" 
                                    HorizontalAlignment="{TemplateBinding
                                    HorizontalContentAlignment}" RecognizesAccessKey="True" 
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="MouseEnter">
                                            <ei:GoToStateAction StateName="LabelGreen"/>
                                        </i:EventTrigger>
                                        <i:EventTrigger EventName="MouseLeave">
                                            <ei:GoToStateAction StateName="LabelAlice"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </ContentPresenter>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    
        </Window.Resources>


    JF Collombet ® CreateSpecificCulture

    jeudi 11 août 2016 11:42