In WPF, when you hover over the button, the standard blue color appears, in Windows 10, it is most likely another. If I remove the checkbox from the IsHitTestVisible parameter, the button stops responding to all my further actions. Actually, I wanted to change the background when pointing the button, but because of this blue color I just don’t see my background. Help me please.

  • one
    Use Simple Styles.xaml - Digital Core

1 answer 1

Before setting any attributes, read about them.

MSDN:

The actual value of this property is affected by the relative positions of the touch-sensitive elements in the logical tree. For example, if an element is a child element of an element that is not visible, the actual property value for the child element will remain false, even if you try to set this value locally. For this reason, it is important not to set IsHitTestVisible for false in a composite control, unless no input data or hits are needed for this control. For more information on hit testing, see the push section on the visual level.

In order to unify styles on all OS, it’s not enough to simply change the background of the elements, you need to write a set of styles, use the basic templates that can be found on MSDN.

You can also use ready-made implementations, which are full on the Internet.

For clarity, here's one of the styles, the result will be:

image

 <!--#region Default Colors--> <Color x:Key="MouseOver.Background.Color">#FFF2F2F2</Color> <Color x:Key="MouseOver.Border.Color">#FFA8B0C2</Color> <Color x:Key="MouseOver.ForegroundHoverColor">#FF696C74</Color> <Color x:Key="Focus.Background.Color">#FFEDEDED</Color> <Color x:Key="Focus.Border.Color">#FF818DA7</Color> <Color x:Key="Focus.Foreground.Color">#FF696C74</Color> <Color x:Key="Pressed.Background.Color">#FFEDEDED</Color> <Color x:Key="Pressed.Border.Color">#FF818DA7</Color> <Color x:Key="Pressed.Foreground.Color">#FF696C74</Color> <Color x:Key="Static.Background.Color">#FFFFFFFF</Color> <Color x:Key="Static.Foreground.Color">#FF696C74</Color> <Color x:Key="Static.Border.Color">#FFBDC3D1</Color> <SolidColorBrush x:Key="Static.Background.Brush" Color="{StaticResource Static.Background.Color}" /> <SolidColorBrush x:Key="Static.Border.Brush" Color="{StaticResource Static.Border.Color}" /> <SolidColorBrush x:Key="Static.Foreground.Brush" Color="{StaticResource Static.Foreground.Color}" /> <SolidColorBrush x:Key="MouseOver.Background.Brush" Color="{StaticResource MouseOver.Background.Color}" /> <SolidColorBrush x:Key="MouseOver.Border.Brush" Color="{StaticResource MouseOver.Border.Color}" /> <SolidColorBrush x:Key="MouseOver.Foreground.Brush" Color="{StaticResource MouseOver.ForegroundHoverColor}" /> <SolidColorBrush x:Key="Pressed.Background.Brush" Color="{StaticResource Pressed.Background.Color}" /> <SolidColorBrush x:Key="Pressed.Border.Brush" Color="{StaticResource Pressed.Border.Color}" /> <SolidColorBrush x:Key="Pressed.Foreground.Brush" Color="{StaticResource Pressed.Foreground.Color}" /> <!--#endregion--> 
 <!--#region FocusVisualButton--> <Style x:Key="BtnFocusVisual"> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate> <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeDashArray="1 2" StrokeThickness="1" /> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--#endregion--> <!--#region DefaultBtn--> <Style x:Key="DefaultBtn" TargetType="{x:Type Button}"> <Setter Property="OverridesDefaultStyle" Value="True" /> <Setter Property="FocusVisualStyle" Value="{DynamicResource BtnFocusVisual}" /> <Setter Property="Padding" Value="12,11,12,10" /> <Setter Property="MinWidth" Value="100" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="RenderOptions.ClearTypeHint" Value="Enabled" /> <Setter Property="RenderOptions.BitmapScalingMode" Value="Fant" /> <Setter Property="FontSize" Value="12" /> <Setter Property="FontFamily" Value="Helvetica Arial sans-serif" /> <Setter Property="FontWeight" Value="Normal" /> <Setter Property="Background" Value="{DynamicResource Static.Background.Brush}" /> <Setter Property="Foreground" Value="{DynamicResource Static.Foreground.Brush}" /> <Setter Property="BorderBrush" Value="{DynamicResource Static.Border.Brush}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="Border" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness=".9" CornerRadius="2"> <ContentPresenter x:Name="PART_Button" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> <VisualStateManager.VisualStateGroups> <VisualStateGroup Name="Common"> <VisualState Name="Normal" /> <VisualState Name="MouseOver"> <Storyboard> <ColorAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="{DynamicResource MouseOver.Background.Color}" /> <ColorAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="(Button.BorderBrush).(SolidColorBrush.Color)" To="{DynamicResource MouseOver.Border.Color}" /> </Storyboard> </VisualState> <VisualState Name="Pressed"> <Storyboard> <ColorAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="{DynamicResource Pressed.Background.Color}" /> <ColorAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="(Button.BorderBrush).(SolidColorBrush.Color)" To="{DynamicResource Pressed.Border.Color}" /> </Storyboard> </VisualState> <VisualStateGroup.Transitions> <VisualTransition GeneratedDuration="00:00:00.2" To="Normal" /> <VisualTransition GeneratedDuration="00:00:00.2" To="MouseOver"> <VisualTransition.GeneratedEasingFunction> <ExponentialEase EasingMode="EaseOut" Exponent="10" /> </VisualTransition.GeneratedEasingFunction> </VisualTransition> <VisualTransition GeneratedDuration="00:00:00.2" To="Pressed"> <VisualTransition.GeneratedEasingFunction> <ExponentialEase EasingMode="EaseOut" Exponent="10" /> </VisualTransition.GeneratedEasingFunction> </VisualTransition> <VisualTransition GeneratedDuration="00:00:00.2" To="Focused"> <VisualTransition.GeneratedEasingFunction> <ExponentialEase EasingMode="EaseOut" Exponent="10" /> </VisualTransition.GeneratedEasingFunction> </VisualTransition> </VisualStateGroup.Transitions> </VisualStateGroup> </VisualStateManager.VisualStateGroups> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value=".6" /> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Cursor" Value="Hand" /> </Trigger> </Style.Triggers> </Style> <!--#endregion--> 

Style application:

 <Button Content="Default Button" Style="{StaticResource DefaultBtn}" /> 

In order not to write style for each button, you can set the unification as follows:

 <!--#region Set DefaultBtn style as Default style for Button--> <Style BasedOn="{StaticResource DefaultBtn}" TargetType="{x:Type Button}" /> <!--#endregion--> 
  • The project does not start because of this line Background = "{TemplateBinding Background}" writes that the Application has passed to the stop state, but there is no code to display on the screen, since all the threads executed external code (usually system code or platform code). - Sergey
  • @Sergey: Where did you write the styles? In general, everything should be fine, I checked it myself, because Styles were written by me. - LLENN
  • Created a resource dictionary called Button and Color and connected these styles via App.xaml - Sergey
  • The fact is that it works in an empty project, but where I need to implement this function does not work. - Sergey
  • @Sergey: This is rather strange, because checked on an existing project, everything worked out well, the error is somewhere with you. - LLENN