Button Image Path is not focusing in wpf - wpf

I have a close button. My requirement is that when a user mouse goes over the button the button background color will change.
I have a path inside the button for "X" symbol. When mouseover the path is the background is changing is working correctly. But very near to mouse over the path then only the background color is changed.
So please could anyone suggest how to resolve this,
MyStyleCode:
<Style x:Key="closeButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="White" />
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="bd">
<Path Margin="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M0,0 L8,8 M8,0 L0,8"
Stroke="White"
StrokeThickness="2" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="true">
<Setter TargetName="bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="bd" Property="Background" Value="Red" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="bd" Property="Background" Value="Red" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Button
<Button Grid.Column="1"
Width="40"
Height="25"
Margin="0,0,5,0"
HorizontalAlignment="Right"
VerticalAlignment="Center"
HorizontalContentAlignment="Center"
Style="{StaticResource closeButtonStyle}"
ToolTip="Close" />

Try setting the Style.Triggers for your Button. Not to your Content alone (Path for your case). Like,
<Style x:Key="closeButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="bd" Background="{TemplateBinding Background}">
<Path Margin="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M0,0 L8,8 M8,0 L0,8"
Stroke="Black"
StrokeThickness="2" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsDefaulted" Value="true">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Red" />
</Trigger>
<Trigger Property="Button.IsPressed" Value="true">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>

Related

How to Set WPF Checkbox Text Colo to Gray if Disabled?

In WPF, when a checkbox is disabled, only the box is grayed out. The text of the checkbox remains black (the default color). How do I make the text also grayed out when checkbox is disabled? The following is the style xaml I've used. The last trigger only changes the box border color, not the text. Target or property does not recognize "Textblock" or "Content". Please help!
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/LightZ;component/Palettes/Brushes.xaml" />
<ResourceDictionary Source="/LightZ;component/Styles/FillBrush.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="CheckBoxFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border>
<Rectangle
Margin="15,0,0,0"
StrokeThickness="1"
Stroke="#60000000"
StrokeDashArray="1 2"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{x:Type CheckBox}" TargetType="CheckBox">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource CheckBoxFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CheckBox">
<BulletDecorator Background="Transparent">
<BulletDecorator.Bullet>
<Border x:Name="Border"
Width="15"
Height="15"
CornerRadius="0"
Background="Transparent"
BorderThickness="1"
BorderBrush="{StaticResource text}">
<Path
Width="12" Height="12"
x:Name="CheckMark"
SnapsToDevicePixels="False"
Stroke="{StaticResource text}"
StrokeThickness="2"
Data="M 0 5 L 3 10 10 0" />
</Border>
</BulletDecorator.Bullet>
<ContentPresenter Margin="4,0,0,0"
VerticalAlignment="Center"
HorizontalAlignment="Left"
RecognizesAccessKey="True"/>
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="false">
<Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter TargetName="CheckMark" Property="Data" Value="M 0 7 L 7 0" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource highlight}" />
<Setter TargetName="CheckMark" Property="Stroke" Value="{StaticResource highlighttext}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource PressedBorderBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Changing the button label foreground on focus with label inside content

I have a button that I need to change the background and foreground color of when the button is in focus. I have the background color change working, but cannot get the foreground color change (on the label) to work.
I want to do this in XAML only.
My Button:
<Button Style="{StaticResource ModButtonWhite}"
Name="btnConnect"
Height="30"
Click="btnConnect_Click"
Width="75"
Margin="0,0,15,0">
<Label Name ="btnConnectLabel" Content="Re-_Connect" />
</Button>
My Style:
<Style TargetType="Button"
x:Key="ModButtonWhite">
<Setter Property="Foreground"
Value="White" />
<Setter Property="Background"
Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}"
CornerRadius="5"
BorderThickness="1"
BorderBrush="White">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsFocused"
Value="True">
<Setter Property="Background"
Value="White" />
<Setter Property="Foreground"
Value="DeepSkyBlue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Resources>
<Style TargetType="Label">
<Setter Property="Foreground"
Value="White" />
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Foreground"
Value="DeepSkyBlue" />
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
Value="White" />
<Setter Property="Opacity"
Value=".4" />
<Setter Property="Foreground"
Value="DeepSkyBlue" />
</Trigger>
</Style.Triggers>
All you need to do is to bind the Label.Foreground to the Button.Foreground. No need of any additional styles defined in the Style.Resources dictionary. Just the plain style to override the Button.Template:
<Window>
<Window.Resources>
<Style TargetType="Button"
x:Key="ModButtonWhite">
<Setter Property="Foreground"
Value="White" />
<Setter Property="Background"
Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}"
CornerRadius="5"
BorderThickness="1"
BorderBrush="White">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsFocused"
Value="True">
<Setter Property="Background"
Value="White" />
<Setter Property="Foreground"
Value="DeepSkyBlue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Button Style="{StaticResource ModButtonWhite}">
<Label Content="Click me!"
Foreground="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}" />
</Button>
</Window>

Displaying hotkey underline on custom styled button

I have changed the styles of my Buttons in WPF. All that is working as intended, but is there a way to get the classic hotkey underscore that you display by pressing ALT to display? If I put an underscore in the Button text, it always displays it.
<Style TargetType="Button">
<Setter Property="FontFamily" Value="{StaticResource THFont}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="#f00000" />
<Setter Property="Height" Value="20" />
<Setter Property="Margin" Value="10 0 10 0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Border x:Name="border" CornerRadius="10" BorderBrush="#f00000" BorderThickness="1" Background="#f00000">
<Grid>
<TextBlock Name="TextBlock" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="12" Margin="10 0 10 0" Text="{TemplateBinding Button.Content}" />
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="border" Value="#BD0000" />
<Setter Property="BorderBrush" TargetName="border" Value="#BD0000" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Your template should include a ContentPresenter:
<Style TargetType="Button">
<Setter Property="FontFamily" Value="{StaticResource THFont}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="#f00000" />
<Setter Property="Height" Value="20" />
<Setter Property="Margin" Value="10 0 10 0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Border x:Name="border" CornerRadius="10" BorderBrush="#f00000" BorderThickness="1" Background="#f00000">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" TextElement.FontSize="12" Margin="10 0 10 0"
Focusable="False" RecognizesAccessKey="True"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="border" Value="#BD0000" />
<Setter Property="BorderBrush" TargetName="border" Value="#BD0000" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

How to create a unique style for toggle buttons with properties changed dynamically in code

I would like to create a style for all toggle buttons in my application. They have all the same behavior except they load different images. My idea was change the resources "ToggleButtonImg_Disabled", "ToggleButtonImg_IsMouseOver", "ToggleButtonImg_IsPressed" and "ToggleButtonImg_IsChecked" in code, when the button is pressed.
However it's not working... Can someone help me! Thanks in advance!
XAML
<Image Width="20" Source="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_Disabled" />
<Image Width="20" Source="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_IsMouseOver" />
<Image Width="20" Source="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_IsPressed" />
<Image Width="20" Source="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_IsChecked" />
<Style TargetType="{x:Type ToggleButton}" x:Key="ToggleButtonStyle">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Content" Value="{DynamicResource ToggleButtonImg_Disabled}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Border
x:Name="CustomControlTemplate"
CornerRadius="2"
BorderThickness="1">
<ContentPresenter
Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Content" Value="{DynamicResource ToggleButtonImg_IsMouseOver}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Content" Value="{DynamicResource ToggleButtonImg_IsPressed}" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="{DynamicResource ToggleButtonImg_IsChecked}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
CODE
//Set the images of the button when the button is pressed
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(#"Resources/Images/icon_play.png", UriKind.Relative);
img.EndInit();
_receiver.Resources["ToggleButtonImg_IsChecked"] = img;
My code updated (only XAML):
<Style TargetType="{x:Type ToggleButton}" x:Key="ToggleButtonStyle">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Content" Value="{DynamicResource ToggleButtonImg_Disabled}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Image x:Name="CustomControlTemplate"
Width="20"
Source="Resources/Images/icon_stop.png"
/>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="CustomControlTemplate" Property="Source" Value="{DynamicResource ToggleButtonImg_IsMouseOver}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="CustomControlTemplate" Property="Source" Value="{DynamicResource ToggleButtonImg_IsPressed}" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="CustomControlTemplate" Property="Source" Value="{DynamicResource ToggleButtonImg_IsChecked}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Add an Image-Control in your ControlTemplate where you want and change the setter of the Triggers to:
<Setter TargetName="imageName" Property="Source" Value="{DynamicResource resourceKey}" />
EDIT: Instead of Images, use BitmapImages as Source:
<BitmapImage Width="20" UriSource="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_Disabled" />
<BitmapImage Width="20" UriSource="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_IsMouseOver" />
<BitmapImage Width="20" UriSource="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_IsPressed" />
<BitmapImage Width="20" UriSource="Resources/Images/icon_stop.png" x:Key="ToggleButtonImg_IsChecked" />

Stylling Button in WPF

I have a style for button as per below code.
<Style TargetType="Button" x:Key="TransparentButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border HorizontalAlignment="Center" x:Name="borderTemplate"
Background="Transparent">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="borderTemplate"
Property="Border.BorderBrush" Value="Gray" />
<Setter TargetName="borderTemplate"
Property="Border.BorderThickness" Value="1" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="borderTemplate"
Property="Border.BorderBrush" Value="Lime" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I need to set the background of button
1.To gray as soon as “MouseUp“ event occurred.
2.And set it to Default(say white)as soon as Focus Of Button being changed or say Focus Being lost.
Is there any way to solve this using Trigger or EventTrigger??.
See One example for above code with image
Button with Above style.
<Button Background="Black" Style="{StaticResource TransparentButton}"
Content="0123456789" Height="15" HorizontalAlignment="Left"
Name="button2" VerticalAlignment="Top" Margin="70,17,0,0" />
Button will look as per below image after above style being applied.
This can be done by using RadioButton instead of Button
just have a look at this
<Style x:Key="RadioButtonStyle1" TargetType="{x:Type RadioButton}">
<Setter Property="Background" Value="#F4F4F4"/>
<Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Border HorizontalAlignment="Center" x:Name="borderTemplate" Background="Transparent" VerticalAlignment="Center">
<ContentPresenter x:Name="contentPresenter" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="borderTemplate" Value="#FFE4E4E4"/>
<Setter Property="HorizontalAlignment" TargetName="contentPresenter" Value="Center"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" TargetName="borderTemplate" Value="#FFA1A1A1"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Grid x:Name="LayoutRoot">
<StackPanel/>
<RadioButton Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="116,86.04,0,0" Style="{StaticResource RadioButtonStyle1}"/>
<RadioButton Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="116,106,0,0" Style="{StaticResource RadioButtonStyle1}"/>
</Grid>

Resources