none
Template sur Window avec évènements WPF. RRS feed

  • Question

  • Bonjour à tous!

    Sur une window, en WPF, j'applique un style dans lequel je définis un template. Dans ce dernier, je crée mes propres boutons de gestion de la fenêtre (fermeture, réduction ...) ainsi que ma propre barre de titre.

    Bien que la solution existe probablement sur internet, j'avoue ne pas l'avoir trouvée (peut-être n'ai-je pas su chercher!)!

    • D'une part, comment, depuis ma window sur laquelle j'applique le style, récupérer les évènements click de mes boutons de gestion de la fenêtre?
    • D'autre part, comment récupérer l'évènement MouseLeftButton de mon StackPanel servant à définir ma barre de titre?

    Merci d'avance pour votre précieuse aide.

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Design="clr-namespace:Mes_Comptes_Bancaires.Design"> <ControlTemplate x:Key="ButtonTemplateCloseMainWindow" TargetType="{x:Type Button}"> <Border x:Name="rootElement" Visibility="Visible" BorderThickness="0" Width="30" Height="20" MinWidth="30" MinHeight="20" MaxWidth="30" MaxHeight="20" ClipToBounds="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"> <Border.Background> <SolidColorBrush x:Name="SolidColorBrush_Background" Color="Transparent"/> </Border.Background> <Grid Margin="0" HorizontalAlignment="Stretch"> <Line x:Name="Line1" X1="10" Y1="5" X2="20" Y2="15" Stretch="None" StrokeThickness="2"> <Line.Stroke> <SolidColorBrush x:Name="SolidColorBrush_Line1" Color="{StaticResource Color_CloseWindowButton_Cross_Normal}"/> </Line.Stroke> </Line> <Line x:Name="Line2" X1="10" Y1="15" X2="20" Y2="5" Stretch="None" StrokeThickness="2"> <Line.Stroke> <SolidColorBrush x:Name="SolidColorBrush_Line2" Color="{StaticResource Color_CloseWindowButton_Cross_Normal}"/> </Line.Stroke> </Line> </Grid> <VisualStateManager.VisualStateGroups> <VisualStateGroup Name="CommonStates"> <VisualStateGroup.Transitions> <VisualTransition From="MouseOver" To="Normal" GeneratedDuration="00:00:0.0"/> <VisualTransition From="MouseOver" To="Pressed" GeneratedDuration="00:00:0.0"/> <VisualTransition From="Normal" To="MouseOver" GeneratedDuration="00:00:0.0"/> </VisualStateGroup.Transitions> <VisualState Name="Normal"/> <VisualState Name="MouseOver"> <Storyboard> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Background" Storyboard.TargetProperty="Color" To="{StaticResource Color_CloseWindowButton_MouseOver}" Duration="00:00:0.0"/> </Storyboard> </VisualState> <VisualState Name="Pressed"> <Storyboard> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Line2" Storyboard.TargetProperty="Color" To="{StaticResource Color_CloseWindowButton_Cross_IsPressed}" Duration="00:00:0.0"/> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Line1" Storyboard.TargetProperty="Color" To="{StaticResource Color_CloseWindowButton_Cross_IsPressed}" Duration="00:00:0.0"/> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Background" Storyboard.TargetProperty="Color" To="{StaticResource Color_CloseWindowButton_Click}" Duration="00:00:0.0"/> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> </Border> <ControlTemplate.Triggers> <Trigger Property="Button.IsEnabled" Value="False"> <Setter TargetName="rootElement" Property="Background"> <Setter.Value> <SolidColorBrush Color="{StaticResource Color_BackgroundColorNotEnabled}"/> </Setter.Value> </Setter> <Setter TargetName="Line1" Property="Stroke"> <Setter.Value> <SolidColorBrush Color="{StaticResource Color_TextColorNotEnabled}"/> </Setter.Value> </Setter> <Setter TargetName="Line2" Property="Stroke"> <Setter.Value> <SolidColorBrush Color="{StaticResource Color_TextColorNotEnabled}"/> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> <ControlTemplate x:Key="ButtonTemplateRestoreMainWindow" TargetType="Button" > <Border x:Name="rootElement" Visibility="Visible" BorderThickness="0" Width="30" Height="20" MinWidth="30" MinHeight="20" MaxWidth="30" MaxHeight="20" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"> <Border.Background> <SolidColorBrush x:Name="SolidColorBrush_Background" Color="{StaticResource Color_BackgroundColor}"/> </Border.Background> <Grid Margin="0" HorizontalAlignment="Stretch" > <Rectangle x:Name="Rectangle1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="12" Height="14"> <Rectangle.Fill> <SolidColorBrush x:Name="SolidColorBrush_Rectangle1" Color="{StaticResource Color_MainColor}"/> </Rectangle.Fill> </Rectangle> <Rectangle x:Name="Rectangle2" Width="9" Height="10" Margin="10,6,11,4"> <Rectangle.Fill> <SolidColorBrush x:Name="SolidColorBruh_Rectangle2" Color="{StaticResource Color_BackgroundColor}"/> </Rectangle.Fill> </Rectangle> </Grid> <VisualStateManager.VisualStateGroups> <VisualStateGroup Name="CommonStates"> <VisualStateGroup.Transitions> <VisualTransition From="MouseOver" To="Normal" GeneratedDuration="00:00:0.0"/> <VisualTransition From="MouseOver" To="Pressed" GeneratedDuration="00:00:0.0"/> <VisualTransition From="Normal" To="MouseOver" GeneratedDuration="00:00:0.0"/> </VisualStateGroup.Transitions> <VisualState Name="Normal"> <Storyboard> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Background" Storyboard.TargetProperty="Color" To="{StaticResource Color_BackgroundColor}" Duration="00:00:0.5"/> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Rectangle1" Storyboard.TargetProperty="Color" To="{StaticResource Color_MainColor}" Duration="00:00:0.5"/> <ColorAnimation Storyboard.TargetName="SolidColorBruh_Rectangle2" Storyboard.TargetProperty="Color" To="{StaticResource Color_BackgroundColor}" Duration="00:00:0.5"/> </Storyboard> </VisualState> <VisualState Name="MouseOver"> <Storyboard> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Background" Storyboard.TargetProperty="Color" To="{StaticResource Color_MainColor}" Duration="00:00:0.2"/> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Rectangle1" Storyboard.TargetProperty="Color" To="{StaticResource Color_BackgroundColor}" Duration="00:00:0.2"/> <ColorAnimation Storyboard.TargetName="SolidColorBruh_Rectangle2" Storyboard.TargetProperty="Color" To="{StaticResource Color_MainColor}" Duration="00:00:0.2"/> </Storyboard> </VisualState> <VisualState Name="Pressed"> <Storyboard> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Background" Storyboard.TargetProperty="Color" To="{StaticResource Color_MainColor}" Duration="00:00:0.0"/> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Rectangle1" Storyboard.TargetProperty="Color" To="{StaticResource Color_ClickColor}" Duration="00:00:0.0"/> <ColorAnimation Storyboard.TargetName="SolidColorBruh_Rectangle2" Storyboard.TargetProperty="Color" To="{StaticResource Color_MainColor}" Duration="00:00:0.0"/> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> </Border> <ControlTemplate.Triggers> <Trigger Property="Button.IsEnabled" Value="False"> <Setter TargetName="rootElement" Property="Background"> <Setter.Value> <SolidColorBrush Color="{StaticResource Color_BackgroundColorNotEnabled}"/> </Setter.Value> </Setter> <Setter TargetName="Rectangle1" Property="Fill"> <Setter.Value> <SolidColorBrush Color="{StaticResource Color_TextColorNotEnabled}"/> </Setter.Value> </Setter> <Setter TargetName="Rectangle2" Property="Fill"> <Setter.Value> <SolidColorBrush Color="{StaticResource Color_BackgroundColorNotEnabled}"/> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> <ControlTemplate x:Key="ButtonTemplateReduceMainWindow" TargetType="Button"> <Border x:Name="rootElement" Visibility="Visible" BorderThickness="0" Width="30" Height="20" MinWidth="30" MinHeight="20" MaxWidth="30" MaxHeight="20" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"> <Border.Background> <SolidColorBrush x:Name="SolidColorBrush_Background" Color="{StaticResource Color_BackgroundColor}"/> </Border.Background> <Grid Margin="0" HorizontalAlignment="Stretch"> <Rectangle x:Name="Line0" HorizontalAlignment="Center" VerticalAlignment="Bottom" Width="13" Height="3" Margin="0,0,0,3"> <Rectangle.Fill> <SolidColorBrush x:Name="SolidColorBrush_Line" Color="{StaticResource Color_MainColor}"/> </Rectangle.Fill> </Rectangle> </Grid> <VisualStateManager.VisualStateGroups> <VisualStateGroup Name="CommonStates"> <VisualStateGroup.Transitions> <VisualTransition From="MouseOver" To="Normal" GeneratedDuration="00:00:0.0"/> <VisualTransition From="MouseOver" To="Pressed" GeneratedDuration="00:00:0.0"/> <VisualTransition From="Normal" To="MouseOver" GeneratedDuration="00:00:0.0"/> </VisualStateGroup.Transitions> <VisualState Name="Normal"> <Storyboard> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Background" Storyboard.TargetProperty="Color" To="{StaticResource Color_BackgroundColor}" Duration="00:00:0.5"/> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Line" Storyboard.TargetProperty="Color" To="{StaticResource Color_MainColor}" Duration="00:00:0.5"/> </Storyboard> </VisualState> <VisualState Name="MouseOver"> <Storyboard> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Background" Storyboard.TargetProperty="Color" To="{StaticResource Color_MainColor}" Duration="00:00:0.2"/> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Line" Storyboard.TargetProperty="Color" To="{StaticResource Color_BackgroundColor}" Duration="00:00:0.2"/> </Storyboard> </VisualState> <VisualState Name="Pressed"> <Storyboard> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Background" Storyboard.TargetProperty="Color" To="{StaticResource Color_MainColor}" Duration="00:00:0.0"/> <ColorAnimation Storyboard.TargetName="SolidColorBrush_Line" Storyboard.TargetProperty="Color" To="{StaticResource Color_ClickColor}" Duration="00:00:0.0"/> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> </Border> <ControlTemplate.Triggers> <Trigger Property="Button.IsEnabled" Value="False"> <Setter TargetName="rootElement" Property="Background"> <Setter.Value> <SolidColorBrush Color="{StaticResource Color_BackgroundColorNotEnabled}"/> </Setter.Value> </Setter> <Setter TargetName="Line0" Property="Fill"> <Setter.Value> <SolidColorBrush Color="{StaticResource Color_TextColorNotEnabled}"/> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> <GradientStopCollection x:Key="DegradeBords"> <GradientStop Offset="0" Color="{StaticResource Color_MainColor}"/> <GradientStop Offset="0.7" Color="{StaticResource Color_HighLightColor}"/> <GradientStop Offset="0.9" Color="White"/> <GradientStop Offset="1" Color="Transparent"/> </GradientStopCollection> <Style x:Key="StyleBordure" TargetType="{x:Type Rectangle}"> <Setter Property="Stroke" Value="Transparent"/> <Setter Property="StrokeThickness" Value="0"/> <Setter Property="VerticalAlignment" Value="{Binding VerticalAlignment}"/> <Setter Property="HorizontalAlignment" Value="{Binding HorizontalAlignment}"/> <Setter Property="Fill"> <Setter.Value> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" GradientStops="{StaticResource DegradeBords}"/> </Setter.Value> </Setter> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Rectangle.HorizontalAlignment" Value="Left"/> <Condition Property="Rectangle.VerticalAlignment" Value="Top"/> </MultiTrigger.Conditions> <Setter Property="Fill"> <Setter.Value> <LinearGradientBrush StartPoint="1,1" EndPoint="0.5,0.5" GradientStops="{StaticResource DegradeBords}"/> </Setter.Value> </Setter> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Rectangle.HorizontalAlignment" Value="Left"/> <Condition Property="Rectangle.VerticalAlignment" Value="Bottom"/> </MultiTrigger.Conditions> <Setter Property="Fill"> <Setter.Value> <LinearGradientBrush StartPoint="1,0" EndPoint="0.5,0.5" GradientStops="{StaticResource DegradeBords}"/> </Setter.Value> </Setter> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Rectangle.HorizontalAlignment" Value="Right"/> <Condition Property="Rectangle.VerticalAlignment" Value="Top"/> </MultiTrigger.Conditions> <Setter Property="Fill"> <Setter.Value> <LinearGradientBrush StartPoint="0,1" EndPoint="0.5,0.5" GradientStops="{StaticResource DegradeBords}"/> </Setter.Value> </Setter> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Rectangle.HorizontalAlignment" Value="Right"/> <Condition Property="Rectangle.VerticalAlignment" Value="Bottom"/> </MultiTrigger.Conditions> <Setter Property="Fill"> <Setter.Value> <LinearGradientBrush StartPoint="0,0" EndPoint="0.5,0.5" GradientStops="{StaticResource DegradeBords}"/> </Setter.Value> </Setter> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Rectangle.HorizontalAlignment" Value="Left"/> <Condition Property="Rectangle.VerticalAlignment" Value="Stretch"/> </MultiTrigger.Conditions> <Setter Property="Fill"> <Setter.Value> <LinearGradientBrush StartPoint="1,0.5" EndPoint="0,0.5" GradientStops="{StaticResource DegradeBords}"/> </Setter.Value> </Setter> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Rectangle.HorizontalAlignment" Value="Right"/> <Condition Property="Rectangle.VerticalAlignment" Value="Stretch"/> </MultiTrigger.Conditions> <Setter Property="Fill"> <Setter.Value> <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5" GradientStops="{StaticResource DegradeBords}"/> </Setter.Value> </Setter> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Rectangle.HorizontalAlignment" Value="Stretch"/> <Condition Property="Rectangle.VerticalAlignment" Value="Top"/> </MultiTrigger.Conditions> <Setter Property="Fill"> <Setter.Value> <LinearGradientBrush StartPoint="0.5,1" EndPoint="0.5,0" GradientStops="{StaticResource DegradeBords}"/> </Setter.Value> </Setter> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Rectangle.HorizontalAlignment" Value="Stretch"/> <Condition Property="Rectangle.VerticalAlignment" Value="Bottom"/> </MultiTrigger.Conditions> <Setter Property="Fill"> <Setter.Value> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" GradientStops="{StaticResource DegradeBords}"/> </Setter.Value> </Setter> </MultiTrigger> </Style.Triggers> </Style> <LinearGradientBrush x:Key="LinearGradientBrush_HeaderMainWindowGradientBrush" EndPoint="0.75,1" StartPoint="0.3,0"> <GradientStop Color="{StaticResource Color_MainColor}" Offset="0"/> <GradientStop Color="{StaticResource Color_LightMainColor}" Offset="0.5"/> <GradientStop Color="{StaticResource Color_BackgroundColor}" Offset="0.6"/> <GradientStop Color="Transparent" Offset="1"/> </LinearGradientBrush> <ControlTemplate x:Key="ControlTemplate_Window" TargetType="{x:Type Window}"> <Grid SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Background="Transparent"> <Grid x:Name="Grid_BorderMainWindow" Background="{TemplateBinding Background}"> <Grid.Resources> <Style TargetType="{x:Type Rectangle}" BasedOn="{StaticResource StyleBordure}"/> </Grid.Resources> <Rectangle x:Name="bottomLeft" Width="4" Height="4" HorizontalAlignment="Left" VerticalAlignment="Bottom" /> <Rectangle x:Name="bottomRight" Width="4" Height="4" VerticalAlignment="Bottom" HorizontalAlignment="Right" /> <Rectangle x:Name="topRight" Width="4" Height="4" HorizontalAlignment="Right" VerticalAlignment="Top" /> <Rectangle x:Name="topLeft" Width="4" Height="4" HorizontalAlignment="Left" VerticalAlignment="Top" /> <Rectangle x:Name="top" Height="4" Margin="4,0,4,0" VerticalAlignment="Top"/> <Rectangle x:Name="bottom" Height="4" Margin="4,0,4,0" VerticalAlignment="Bottom" /> <Rectangle x:Name="left" Width="4" Margin="0,4,0,4" HorizontalAlignment="Left" /> <Rectangle x:Name="right" Width="4" Margin="0,4,0,4" HorizontalAlignment="Right" /> </Grid> <Grid x:Name="Grid_MainWindow" Margin="4" Background="{TemplateBinding Background}"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Design:FondMainForm Grid.Row="0" Grid.RowSpan="2"/> <Rectangle Grid.Row="0" Grid.RowSpan="2" Fill="Transparent" StrokeThickness="1"> <Rectangle.Stroke> <SolidColorBrush Color="{StaticResource Color_MainColor}"/> </Rectangle.Stroke> </Rectangle> <Grid x:Name="Grid_HeaderMainWindow" Grid.Row="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <StackPanel Orientation="Horizontal" Grid.Column="0" Cursor="ScrollAll" Margin="1,1,1,0" Background="{StaticResource LinearGradientBrush_HeaderMainWindowGradientBrush}" Height="{Binding ElementName=StackPanel_ButtonsWindow,Path=Height}"> <TextBlock x:Name="TextBlock_Title" HorizontalAlignment="Left" VerticalAlignment="Center" Text="{TemplateBinding Title}" Margin="10,0,0,0"> <TextBlock.Foreground> <SolidColorBrush Color="{StaticResource Color_BackgroundColor}"/> </TextBlock.Foreground> </TextBlock> </StackPanel> <StackPanel x:Name="StackPanel_ButtonsWindow" HorizontalAlignment="Right" VerticalAlignment="Top" Orientation="Horizontal" Grid.Column="1" Margin="0,1,1,0"> <!--<Button x:Name="Button_Deconnexion" Content="Déconnexion" Visibility="Visible" Height="20" Margin="2" Template="{DynamicResource ButtonTemplateGeneral}" Background="Blue"/>--> <Button Content="Réduire" Margin="0" Template="{StaticResource ButtonTemplateReduceMainWindow}"/> <Button Content="Agrandir" Margin="0" Template="{StaticResource ButtonTemplateRestoreMainWindow}"/> <Button Content="Quitter" Margin="0" Template="{StaticResource ButtonTemplateCloseMainWindow}"/> </StackPanel> </Grid> <Grid x:Name="Grid_ContentMainWindow" Grid.Row="1"> <ContentPresenter Content="{TemplateBinding Content}"/> </Grid> </Grid> </Grid> </ControlTemplate> <Style x:Key="Style_Window" TargetType="{x:Type Window}"> <Setter Property="MinHeight" Value="500"/> <Setter Property="MinWidth" Value="737"/> <Setter Property="WindowStyle" Value="None"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="AllowsTransparency" Value="True"/> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="Template" Value="{StaticResource ControlTemplate_Window}"/> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> <Setter Property="VerticalContentAlignment" Value="Stretch"/> </Style> </ResourceDictionary>


    lundi 10 juin 2013 18:18

Réponses

  • J'ai trouvé une solution, peut-être pas la meilleur....

    Dans le code behind de la window, dans son constructeur, après InitializeComponent(), j'ajoute

    // Constructeur. public Window() { InitializeComponent(); this.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.ButtonsWindow_click)); // Récupération des évènements click sur les boutons. // Même principe pour récupérer le MouseLeftButton. // ... } // Récupération d'un click sur un bouton. private void ButtonsWindow_click(object sender, RoutedEventArgs e) { if("Verification si c'est le bouton fermeture par exemple") { this.Close(); } // else ... }


    mardi 11 juin 2013 11:10

Toutes les réponses