I've created a style for buttons in my WPF application.
However, on Vista, when a button is focused, it pulsates with a light blue that doesn't look good in the design.
How can I override this automatic pulsating?
<Style x:Key="NavigationButtonStyle" TargetType="Button">
<Setter Property="Width" Value="400"/>
<Setter Property="Height" Value="100"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="FontSize" Value="56"/>
<Setter Property="Foreground" Value="#aaa"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#ddd" />
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Foreground" Value="#aaa" />
</Trigger>
</Style.Triggers>
</Style>
Tried "IsFocused":
Adding this doesn't have any effect on Vista, the button still looks the same and is pulsating in Vista when focused:
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Blue" />
</Trigger>
Tried "ControlTemplate" solution:
Still no effect:
<Style x:Key="ButtonFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle SnapsToDevicePixels="true" Margin="2" Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="NavigationButtonStyle" TargetType="Button">
<Setter Property="Width" Value="400"/>
<Setter Property="Height" Value="100"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="FontSize" Value="56"/>
<Setter Property="Foreground" Value="#aaa"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#ddd" />
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Foreground" Value="#aaa" />
</Trigger>
</Style.Triggers>
</Style>
This effect is being set in response IsKeyboardFocused, not just standard focus.
In Blend if you edit the control template for a standard button, you'll see that on the IsKeyboardFocused=true trigger is responsible for this effect.
<Style x:Key="Button_NonGlow" TargetType="{x:Type Button}">
<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 Button}">
<Microsoft_Windows_Themes:ButtonChrome SnapsToDevicePixels="true" x:Name="Chrome" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding IsDefaulted}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
</Microsoft_Windows_Themes:ButtonChrome>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="true">
<Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
What you are looking for is the IsFocused trigger property. Sorry that I do not have a working example for you right here.
UPDATE:
I found this small piece of code that I once used. I am not sure if is works and I can not test it (I am on an XP box):
<Style x:Key="ButtonFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle SnapsToDevicePixels="true" Margin="2" Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
</Style>
Depending on your specific requirements you can simply ensure the button never gets keyboard focus by setting IsTabStop="False" and IsDefault="False".
Related
I have a little WPF I am creating (Self taught).
It was created in VS2021 I believe however my PC died (Motherboard).
I have reinstalled everything on a new PC and brought over the backups and have since installed VS2022.
Upon opening I am getting heaps of errors (30) on my app.xml style section.
This was working fine and weirdly still will run and compile but obviously I'm not doing something right.
Interstingly some sections are working and others aren't.
The main ones not working are referencing the SOLIDCOLORBRUSH at the top which is setting my colours for the app.
the main errors are:
Error XDG0062 'StaticResourceExtension' is not valid for Setter.Value.
The only supported MarkupExtension types are DynamicResourceExtension
and BindingBase or derived types.
Error XDG0062 The resource "brdrTeal" has an incompatible type.
Error XDG0062 'txtTitle' resource not found.
These are all referenced with {StaticResource XXXXX}
Finally this section has this error:
Error:
Error XDG0062 '{DependencyProperty.UnsetValue}' is not a valid value
for the 'System.Windows.Controls.Control.Template' property on a
Setter.
Code:
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="Border" Value="{StaticResource Teal2}"/>
<Setter Property="BorderBrush" TargetName="Border" Value="#00827b"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="Border" Value="{StaticResource Teal2}"/>
<Setter Property="BorderBrush" TargetName="Border" Value="#00827b"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I've included a chunk of the app.xaml file below, to see how I have laid everything out.
Keen to do this properly if this is an incorrect way to do this, as I meantioned I'm just learning this now.
<Application.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="Teal1" Color="#67DAD5" />
<SolidColorBrush x:Key="Teal2" Color="#39c0c0" />
<SolidColorBrush x:Key="Teal3" Color="#00bab3" />
<SolidColorBrush x:Key="Teal4" Color="#00a09b" />
<SolidColorBrush x:Key="Teal5" Color="#00827b" />
<SolidColorBrush x:Key="mintGreen" Color="#a4e9c6" />
<SolidColorBrush x:Key="Teal2Light" Color="#c9e9ea" />
<SolidColorBrush x:Key="LightGrey" Color="#f6f6f6" />
<SolidColorBrush x:Key="MidGrey" Color="#aaaaad" />
<SolidColorBrush x:Key="DarkGrey" Color="#56565b" />
<SolidColorBrush x:Key="Burgundy1" Color="#8C0F57" />
<SolidColorBrush x:Key="Burgundy2" Color="#deb9c7" />
<SolidColorBrush x:Key="activeAreaColour" Color="#c9e9ea"/>
<Style TargetType="{x:Type Window}">
<Setter Property="ShowInTaskbar" Value="false"/>
</Style>
<Style TargetType="{x:Type Image}" x:Key="toolTipImage">
<Setter Property="RenderOptions.BitmapScalingMode" Value="Fant" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Height" Value="20"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="10 10 0 0"/>
</Style>
<Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}" >
<Setter Property="Background" Value="{DynamicResource Teal2}" />
<Setter Property="Foreground" Value="white" />
<Setter Property="BorderBrush" Value="{DynamicResource Teal5}"/>
<Setter Property="BorderThickness" Value="1 1 1 1"/>
<Setter Property="Margin" Value="-1,-1,0,0" />
<Setter Property="Height" Value="28" />
<Setter Property="Width" Value="auto"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
<Style TargetType="DataGridCell">
<Setter Property="HorizontalContentAlignment" Value="Right" />
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource Teal2Light}" />
<Setter Property="BorderBrush" Value="{DynamicResource Teal5}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Foreground" Value="black" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontSize" Value="12"/>
</Trigger>
</Style.Triggers>
</Style>
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Border
x:Name="Border"
Grid.ColumnSpan="2"
CornerRadius="2"
Background="{DynamicResource Teal5}"
BorderBrush="{DynamicResource Teal5}"
BorderThickness="2" />
<Border x:Name="lrgBorder"
Grid.Column="0"
CornerRadius="2,0,0,2"
Margin="1"
Background="{DynamicResource Teal2Light}"
BorderBrush="{DynamicResource Teal2Light}"
BorderThickness="0,0,1,0" />
<Path
x:Name="Arrow"
Grid.Column="1"
Fill="White"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ToggleButton.IsMouseOver" Value="true">
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource Burgundy1}" />
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource Teal5}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background" Value="{StaticResource Burgundy2}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource Burgundy1}" />
<Setter TargetName="lrgBorder" Property="Background" Value="{StaticResource LightGrey}" />
<Setter Property="Foreground" Value="{StaticResource Burgundy1}"/>
<Setter TargetName="Arrow" Property="Fill" Value="{StaticResource Burgundy2}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
<Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
</ControlTemplate>
<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="MinWidth" Value="120"/>
<Setter Property="MinHeight" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton
Name="ToggleButton"
Template="{StaticResource ComboBoxToggleButton}"
Grid.Column="2"
Focusable="false"
IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press">
</ToggleButton>
<ContentPresenter
Name="ContentSite"
IsHitTestVisible="False"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
Margin="3,3,23,3"
VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBox x:Name="PART_EditableTextBox"
Style="{x:Null}"
Template="{StaticResource ComboBoxTextBox}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="3,3,23,3"
Focusable="True"
Background="Transparent"
Visibility="Hidden"
IsReadOnly="{TemplateBinding IsReadOnly}"/>
<Popup
Name="Popup"
Placement="Bottom"
IsOpen="{TemplateBinding IsDropDownOpen}"
AllowsTransparency="True"
Focusable="False"
PopupAnimation="Slide">
<Grid
Name="DropDown"
SnapsToDevicePixels="True"
MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border
x:Name="DropDownBorder"
Background="{StaticResource Teal2Light}"
BorderThickness="1"
BorderBrush="{StaticResource Teal5}"/>
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{StaticResource Burgundy1}"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
<Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
</Trigger>
<Trigger Property="IsEditable"
Value="true">
<Setter Property="IsTabStop" Value="false"/>
<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
<Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="Border" Value="{StaticResource Teal2}"/>
<Setter Property="BorderBrush" TargetName="Border" Value="#00827b"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#c9e9ea" Offset="0.0"/>
<GradientStop Color="#67DAD5" Offset="1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
<Style x:Key="activeBackground" TargetType="Grid">
<Setter Property="Background" Value="{DynamicResource Teal2}"/>
</Style>
<sys:Double x:Key="fntButton">18</sys:Double>
<sys:Double x:Key="fntButtonSmall">12</sys:Double>
<sys:Double x:Key="ftnTextBox">15</sys:Double>
<Style x:Key="brdrTeal" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="{StaticResource Teal2}"/>
<Setter Property="BorderThickness" Value="1"/>
</Style>
<Style x:Key="activeArea" TargetType="{x:Type Grid}">
<Setter Property="Background" Value="{StaticResource activeAreaColour}"/>
</Style>
<Style x:Key="brdrBurgundy" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="{StaticResource Burgundy1}"/>
<Setter Property="BorderThickness" Value="1"/>
</Style>
<Style TargetType="{x:Type Label}">
<Setter Property="Margin" Value="0 10 0 0"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="FontWeight" Value="DemiBold"/>
</Style>
<!--#region TxtBoxes and passwordbox-->
<Style x:Key="txtWhite" TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="{StaticResource ftnTextBox}"/>
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="{StaticResource brdrTeal}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Margin" Value="5"/>
</Style>
<Style x:Key="txtGrey" TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="{StaticResource ftnTextBox}"/>
<Setter Property="Background" Value="{StaticResource LightGrey}"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="5 0 0 0"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Margin" Value="0 10 0 0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="false">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource ResourceKey=MidGrey}"/>
<Setter Property="BorderThickness" TargetName="border" Value="2"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource ResourceKey=Teal2}"/>
<Setter Property="BorderThickness" TargetName="border" Value="2"/>
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource ResourceKey=Teal2}"/>
<Setter Property="Background" TargetName="border" Value="{StaticResource ResourceKey=Teal2Light}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{StaticResource ResourceKey=Burgundy1}"/>
<Setter Property="FontWeight" Value="DemiBold"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="txtPassGrey" TargetType="{x:Type PasswordBox}">
<Setter Property="FontSize" Value="{StaticResource ftnTextBox}"/>
<Setter Property="Background" Value="{StaticResource LightGrey}"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type PasswordBox}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource ResourceKey=Teal2}"/>
<Setter Property="BorderThickness" TargetName="border" Value="2"/>
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource ResourceKey=Teal2}"/>
<Setter Property="Background" TargetName="border" Value="{StaticResource ResourceKey=Teal2Light}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{StaticResource ResourceKey=Burgundy1}"/>
<Setter Property="FontWeight" Value="DemiBold"/>
</Trigger>
</Style.Triggers>
</Style>
<!--#endregion -->
</ResourceDictionary>
</Application.Resources>
BorderBrush color of my button template does not change to Yellow. Please help.
<Style x:Key="someName" TargetType="{x:Type Button}">
<Setter Property="Background" Value="LightGreen"/>
<Setter Property="BorderBrush" Value="Yellow"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
You have to bind the BorderBrush to the templated control via TemplateBinding so that it applies the value defined in your style. Additionally, you have to set add a BorderThickness of at least one the same way, otherwise there is no border.
<Style x:Key="someName" TargetType="{x:Type Button}">
<Setter Property="Background" Value="LightGreen"/>
<Setter Property="BorderBrush" Value="Yellow"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
As a general note, a control template defines all parts and states of a control and transitions between them. Your control template only defines the Mouse Over state apart from the normal state. Missing states may harm the user experience. You can find a list of all parts and states for a button in the documentation.
It is recommendable to extract the default control template or style for a control with Blend or Visual Studio first and adapting that. I have extracted the default style and merged it with your style. You just have to adapt the Disabled and Pressed colors.
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
<Setter Property="Background" Value="LightGreen"/>
<Setter Property="BorderBrush" Value="Yellow"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="true">
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="Green"/>
<Setter Property="BorderBrush" TargetName="border" Value="Yellow"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="border" Value="#FFC4E5F6"/>
<Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/>
<Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="TextInputStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="5,5,5,5"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Background" Value="Red"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="bg" BorderBrush="#FF7F98DC" BorderThickness="1">
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="#FF7E97F0"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="DarkBlue"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This is a part of my App.xaml code.
It is style for textBox.
But Background Property didn't work.
Others work well.
Please help me. Why can't I change background color of TextBox?
You are giving a background to your text box, instead you should give the background to your border because it has been defined in the control template.
<Style x:Key="TextInputStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="FontSize" Value="12"/>
<Setter Property="Margin" Value="5,5,5,5"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Foreground" Value="AliceBlue"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="bg" BorderBrush="#FF7F98DC" BorderThickness="1" ***Background="Red"***>
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="#FF7E97F0"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="DarkBlue"/>
<Setter Property="BorderThickness" TargetName="bg" Value="2"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Please see the Border tag inside ControlTemplate above.
I have various text boxes in my application, where i have used a red border to validate the data being entered. the problem is when i change tabs the red border disappears. So i am trying to apply AdornedElementPlaceholder in my styles file so that all the textboxes can adapt that behaviour. I have tried the below mentioned code but the line of code in bold (AdornedElementPlaceholder) is not working. I am using WPF and C#. Can anyone please help?
<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}" >
<Setter Property="Width" Value="120" />
<Setter Property="Height" Value="25" />
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border x:Name="bg"
CornerRadius="5"
Padding="2"
Background="{TemplateBinding Background}"
BorderBrush="{StaticResource NormalBorderBrush}"
BorderThickness="1" >
**<AdornedElementPlaceholder/>**
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush"
TargetName="bg" Value="#82CAFA"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush"
TargetName="bg" Value="#1589FF"/>
</Trigger>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="Background"
Value="#E5E4E2"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="SpellCheck.IsEnabled" Value="True" />
<Setter Property="Language" Value="en-gb" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="BorderBrush" Value="{StaticResource
ErrorBackground}" />
<Setter Property="ToolTip" Value="{Binding
RelativeSource={RelativeSource Self}, Path=
(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="{StaticResource NormalBorderBrush}"/>
<Setter Property="Width" Value="120"/>
<Setter Property="Height" Value="25"/>
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="SpellCheck.IsEnabled" Value="True"/>
<Setter Property="Language" Value="en-gb"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border CornerRadius="5" Padding="2" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">
<ScrollViewer x:Name="PART_ContentHost" Padding="{TemplateBinding Padding}" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="#82CAFA"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" Value="#1589FF"/>
</Trigger>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="Background" Value="#E5E4E2"/>
</Trigger>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="BorderBrush" Value="{StaticResource ErrorBackground}"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
I'm trying to make custom buttons for my application but it's not going very well.
I have the following XAML just for testing purposes:
<Style x:Key="DefBtn" TargetType="{x:Type Button}">
<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="Button">
<Border Name="border"
BorderThickness="1"
Padding="4,2"
BorderBrush="DarkGray"
CornerRadius="3"
Background="{TemplateBinding Background}">
<Grid >
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" Name="content"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="Yellow" />
<Setter Property="Foreground" Value="#FF4788c8" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
-
<Button x:Name="btnCancelTimer" Content="{Binding [LangResources.timerDetail4], FallbackValue=Cancel Timer, Mode=OneWay, Source={StaticResource localisation}}" Margin="231,0,231,32.04" VerticalAlignment="Bottom" IsCancel="True" FontSize="13.333" Click="BtnCancelTimerClick" Cursor="Hand" Foreground="#FF666666" Background="White" FontFamily="Arial" FontWeight="Bold" Width="115" Height="24" Style="{DynamicResource DefBtn}" />
I'm trying to achieve a button styling exactly like this, also including mouseover and pressed stylings on the button.
Can anyone help/guide me on how I could achieve this?
Thanks
Try using this style
<Style x:Key="DefBtn" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#FFF5F5F5"/>
<Setter Property="BorderBrush" Value="#FFDCDCDC"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="#FF666666"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="11"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="8,7"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
BorderBrush="{TemplateBinding BorderBrush}"
CornerRadius="1"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<!--TODO: Set the right colors-->
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="Yellow" />
<Setter Property="Foreground" Value="#FF4788c8" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<!--Some setters here-->
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I don't have too keen of an eye for fonts, but Arial seems to be very close. KaXaml shows this style as follows:
Also, you need to check the setters for the MouseOver, Pressed and Disabled (IsEnabled=false) triggers\states.