none
Problème de surbrillance avec un Bouton au passage de la souris RRS feed

  • Question

  • Bonjour a tous,

    Je voudrais savoir si il est possible de désactiver la surbrillance d'un bouton (en couleur) au passage de la souris.

    En ayant mis Focusable = False

    merci, 

    vendredi 20 avril 2012 15:35

Réponses

  • Bonjour,

    Je peux me tromper mais cette surbrillance est du au style du boutton par défaut de WPF et je ne pense pas qu'une propriété puisse y changer quelque chose.

    Voici le code xaml du style du button par défaut :

    <Style x:Key="BaseButtonStyle"
               TargetType="{x:Type ButtonBase}">
                    <Setter Property="FocusVisualStyle"
                    Value="{StaticResource ButtonFocusVisual}"/>
                    <Setter Property="Background"
                    Value="{StaticResource ButtonNormalBackground}"/>
                    <Setter Property="BorderBrush"
                    Value="{StaticResource ButtonNormalBorder}"/>
                    <Setter Property="BorderThickness"
                    Value="1"/>
                    <Setter Property="Foreground"
                    Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                    <Setter Property="HorizontalContentAlignment"
                    Value="Center"/>
                    <Setter Property="VerticalContentAlignment"
                    Value="Center"/>
                    <Setter Property="Padding"
                    Value="1"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ButtonBase}">
                                <theme:ButtonChrome Name="Chrome"
                                            Background="{TemplateBinding Background}"
                                            BorderBrush="{TemplateBinding BorderBrush}"
                                            RenderDefaulted="{TemplateBinding Button.IsDefaulted}"
                                            RenderMouseOver="{TemplateBinding IsMouseOver}"
                                            RenderPressed="{TemplateBinding IsPressed}"
                                            SnapsToDevicePixels="true">
                                    <ContentPresenter Margin="{TemplateBinding Padding}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              RecognizesAccessKey="True"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </theme:ButtonChrome>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsKeyboardFocused"
                                     Value="true">
                                        <Setter TargetName="Chrome"
                                        Property="RenderDefaulted"
                                        Value="true"/>
                                    </Trigger>
                                    <Trigger Property="ToggleButton.IsChecked"
                                     Value="true">
                                        <Setter TargetName="Chrome"
                                        Property="RenderPressed"
                                        Value="true"/>
                                    </Trigger>
                                    <Trigger Property="IsEnabled"
                                     Value="false">
                                        <Setter Property="Foreground"
                                        Value="#ADADAD"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
    

    Donc dans ton cas il faut redéfinir ton style des boutons (dans mon exemple j'utilise le style aero) :

    voici un exemple que tu peux tester :

    <Window x:Class="WpfApplication20.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.Resources>
    
                <Style x:Key="ButtonFocusVisual">
                    <Setter Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Rectangle Margin="2"
                                   StrokeThickness="1"
                                   Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                                   StrokeDashArray="1 2"
                                   SnapsToDevicePixels="true"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
    
    
                <LinearGradientBrush x:Key="ButtonNormalBackground"
                             StartPoint="0,0"
                             EndPoint="0,1">
                    <LinearGradientBrush.GradientStops>
                        <GradientStop Color="#F3F3F3"
                              Offset="0"/>
                        <GradientStop Color="#EBEBEB"
                              Offset="0.5"/>
                        <GradientStop Color="#DDDDDD"
                              Offset="0.5"/>
                        <GradientStop Color="#CDCDCD"
                              Offset="1"/>
                    </LinearGradientBrush.GradientStops>
                </LinearGradientBrush>
                <SolidColorBrush x:Key="ButtonNormalBorder"
                         Color="#FF707070"/>
                
                <Style x:Key="BaseButtonStyle"
               TargetType="{x:Type ButtonBase}">
                    <Setter Property="FocusVisualStyle"
                    Value="{StaticResource ButtonFocusVisual}"/>
                    <Setter Property="Background"
                    Value="{StaticResource ButtonNormalBackground}"/>
                    <Setter Property="BorderBrush"
                    Value="{StaticResource ButtonNormalBorder}"/>
                    <Setter Property="BorderThickness"
                    Value="1"/>
                    <Setter Property="Foreground"
                    Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                    <Setter Property="HorizontalContentAlignment"
                    Value="Center"/>
                    <Setter Property="VerticalContentAlignment"
                    Value="Center"/>
                    <Setter Property="Padding"
                    Value="1"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ButtonBase}">
                                <theme:ButtonChrome Name="Chrome"
                                            Background="{TemplateBinding Background}"
                                            BorderBrush="{TemplateBinding BorderBrush}"
                                            RenderDefaulted="{TemplateBinding Button.IsDefaulted}"
                                            
                                            RenderPressed="{TemplateBinding IsPressed}"
                                            SnapsToDevicePixels="true">
                                    <ContentPresenter Margin="{TemplateBinding Padding}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              RecognizesAccessKey="True"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </theme:ButtonChrome>
                                <ControlTemplate.Triggers>
                                    
                                    <Trigger Property="ToggleButton.IsChecked"
                                     Value="true">
                                        <Setter TargetName="Chrome"
                                        Property="RenderPressed"
                                        Value="true"/>
                                    </Trigger>
                                    <Trigger Property="IsEnabled"
                                     Value="false">
                                        <Setter Property="Foreground"
                                        Value="#ADADAD"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
    
    
            </Grid.Resources>
            <Button  Content="test" Click="Button_Click" Style="{StaticResource ResourceKey=BaseButtonStyle}"/>
        </Grid>
    </Window>
    

    et le code C# pour tester si la pression sur le bouton fonctionne bien :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfApplication20
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("test");
            }
        }
    }


    Cordialement, Pascal.

    Développeur Wpf/SilverLight/WinPhone7

    dimanche 22 avril 2012 17:34
    Modérateur