WPF customized TextBox - wpf

I have a customized TextBox named "txtAddress" which contains Image and TextBox.
I need to be able to access its contents : txtAddress.Text txtAddress.Image
I've Bound the inner textBox.text with the Template.Text and inner rect.Fill with Template.Background (I'm using rectangular now, i will change it to image)
when I run the program and edit the textBox everything looks like its working and the text is changing, but when i use it from the code txtAddress.Text is still "my computer" which is the initial value of the TextBox.
I know that because i didn't put <ContentPresenter> somewhere in the style but where should i put the controls(image,textbox), like in listView there is <DataTemplate> where we add the controls
TxtAddress Style:
<Style x:Key="TextBoxAddressStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Stretch" Width="Auto">
<Rectangle Height="Auto" Stroke="{TemplateBinding Foreground}" VerticalAlignment="Stretch" RadiusY="11" RadiusX="11" Margin="0" Fill="{TemplateBinding OpacityMask}"/>
<!--<ContentPresenter Height="Auto" Margin="31,2,7.458,2"/>-->
<TextBox x:Name="TxtAddress" Height="Auto" Margin="31,2,7.458,2" VerticalAlignment="Stretch" MaxLines="1" Text="{TemplateBinding Text}" AcceptsTab="True" Style="{DynamicResource TextBoxStyle2}" SelectionBrush="#C859003D" FontSize="{TemplateBinding FontSize}" AcceptsReturn="False"/>
<Rectangle Fill="#5AFFFFFF" HorizontalAlignment="Stretch" Height="6.375" Margin="6,2.25,6,0" RadiusY="6" RadiusX="6" Stroke="{x:Null}" VerticalAlignment="Top" Width="Auto" d:IsLocked="True"/>
<Rectangle x:Name="ImgAddress" Fill="{TemplateBinding Background}" HorizontalAlignment="Left" Height="24" Margin="7,1,0,0" RadiusY="0" RadiusX="0" VerticalAlignment="Top" Width="24" StrokeThickness="0">
<Rectangle.Stroke>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="1"/>
<GradientStop Color="White"/>
</LinearGradientBrush>
</Rectangle.Stroke>
</Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
<Condition Property="IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
</MultiTrigger>
</Style.Triggers>
</Style>
Inner TextBox Style:
<Style x:Key="TextBoxStyle2" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" FontSize="13.333" PanningMode="HorizontalOnly"/>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
<Condition Property="IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
</MultiTrigger>
</Style.Triggers>
</Style>
TxtAddress :
<TextBox x:Name="txtAddress" Margin="34,5,32,0" TextWrapping="Wrap" Text="My Computer" Style="{DynamicResource TextBoxAddressStyle}" Height="25" VerticalAlignment="Top" FontWeight="Bold" MinHeight="25" MaxHeight="25">
<TextBox.Background>
<ImageBrush ImageSource="BtnImg/computer.png" Stretch="None"/>
</TextBox.Background>
<TextBox.Foreground>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF779198" Offset="0.1"/>
<GradientStop Color="#FF789399" Offset="0.93"/>
<GradientStop Color="#FFBFD3D7" Offset="0.513"/>
<GradientStop Color="#FF343E41" Offset="1"/>
<GradientStop Color="#FF5C6E73"/>
</LinearGradientBrush>
</TextBox.Foreground>
<TextBox.OpacityMask>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF94B5BD" Offset="0.1"/>
<GradientStop Color="#FF94B5BD" Offset="0.93"/>
<GradientStop Color="#FFE7FBFF" Offset="0.513"/>
<GradientStop Color="#FF7B9399" Offset="1"/>
<GradientStop Color="#FF7B9399"/>
</LinearGradientBrush>
</TextBox.OpacityMask>
</TextBox>
thanks in advance.

You're placing a TextBox inside the Template for another TextBox.
The visual tree will look like this:
TextBox
Grid
Rectangle
TextBox
Rectangle
Rectangle
Which is not ideal in the first place, but if you want to keep things like that, just make sure you bind the inner TextBox's Text property to the outer one, via Two Way, UpdateSourceTrigger=PropertyChanged Data Binding like so:
<TextBox x:Name="TxtAddress" Text="{Binding Text,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}" ... />
In addition to all this, I already mentioned several times that you're NOT supposed to be manipulating UI elements in procedural code in WPF.
Instead, create a proper ViewModel to store your data:
public class MyViewModel
{
public string Text {get;set;}
}
then use DataBinding to bind your TextBox to that data:
<TextBox Text="{Binding Text}"/>
And whenever you need to retrieve the value, retrieve the value from the VM, NOT the UI.

Related

Toggle button in WPF

I am new to wpf . I want to create toggle button like
how can I achieve this. should I need to use two buttons and on click of each I need to disable other one. or there is anything else like toggle button in wpf. what is best way to achieve this.. any suggestion appreciated.Thanks
Here is a quick Version. The trick is to use a style.
Style:
<Style x:Key="ButtonFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#F3F3F3" Offset="0"/>
<GradientStop Color="#EBEBEB" Offset="0.5"/>
<GradientStop Color="#DDDDDD" Offset="0.5"/>
<GradientStop Color="#CDCDCD" Offset="1"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
<Style x:Key="ToggleButtonStyle1" TargetType="{x:Type ToggleButton}">
<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 ToggleButton}">
<StackPanel Orientation="Horizontal">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<Border x:Name="on" Width="25" Height="25" Background="LightGray" CornerRadius="2,0,0,4" Margin="10,0,0,0">
<TextBlock x:Name="onText" Text="On" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<Border x:Name="off" Width="25" Height="25" Background="LightGray" CornerRadius="0,2,4,0">
<TextBlock x:Name="offText" Text="Off" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
<Trigger Property="IsChecked" Value="true">
<Setter TargetName="on" Property="Background" Value="LightBlue"/>
<Setter TargetName="onText" Property="Foreground" Value="White"/>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="off" Property="Background" Value="LightBlue"/>
<Setter TargetName="offText" Property="Foreground" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
ToggleButton call:
<ToggleButton
Content="ON LINE MODE"
Style="{StaticResource ToggleButtonStyle1}"/>
Preview

WPF - Content Presenter

I have created a resource dictionary for Button style and assigned it to a Button. The style appears in the button but I am not able to get the button text displayed. I have tried adding a content presenter but it didn't work. Please help.
<Button x:Name ="Submit" HorizontalAlignment="Left" Margin="346,186,0,0"
VerticalAlignment="Top" Width="75" Style="{DynamicResource
ResourceKey=ButtonStyle}" Height="50">
<ContentPresenter Name="MyContent">
<ContentPresenter.Content>
<Label>Click Me</Label>
</ContentPresenter.Content>
</ContentPresenter>
</Button>
The style for button is as follows..It is created using Blend..
<Style x:Key="ButtonStyle" 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}">
<Grid>
<Rectangle Margin="0,8,0,0" VerticalAlignment="Stretch" Stroke="#FF000000" RadiusX="23.489" RadiusY="23.489">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FFDE9090" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true"/>
<Trigger Property="ToggleButton.IsChecked" Value="true"/>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I also tried Button.Content> Click Button.Content> but not working.. Please suggest
Your button's ControlTemplate in Style missing a ContentPresenter to render the Content.
Add a ContentPresenter to the button's ControlTemplate. The resulting XAML for ControlTemplate would look like:
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle Margin="0,8,0,0" VerticalAlignment="Stretch" Stroke="#FF000000" RadiusX="23.489" RadiusY="23.489">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FFDE9090" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true"/>
<Trigger Property="ToggleButton.IsChecked" Value="true"/>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Also, like I said in my comment, you do not need a ContentPresenter in the XAML for Button itself. You could simply have your content between and tags.
<Button
x:Name="Submit"
Width="75"
Height="50"
HorizontalAlignment="Left"
Margin="346,186,0,0"
VerticalAlignment="Top"
Style="{DynamicResource ResourceKey=ButtonStyle}">
Click Me
</Button>
Read this MSDN page for more information on ControlTemplate.

WPF - selected unfocused color of ListViewItem with a GridView

I'm trying to change the default light gray highlight on a selected ListViewItem to be the blue highlight that shows up when the ListView is focused. I've been trying to reconcile different StackOverflow answers and sources online, but I haven't figured out what kind of XAML I need yet. I have the following:
<ListView ItemContainerStyle="{StaticResource checkableListViewItem}"
SelectionMode="Multiple"
View="{StaticResource fieldValueGridView}"/>
The referenced View resource is:
<GridView x:Key="fieldValueGridView" AllowsColumnReorder="False">
<GridViewColumn Header="Field">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock FontWeight="Bold" Text="{Binding Path=DisplayName}"/>
<TextBlock FontWeight="Bold" Text=": "/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=FieldValue}"/>
</GridView>
And the referenced ItemContainerStyle resource is:
<Style TargetType="ListViewItem" BasedOn="{StaticResource {x:Type ListViewItem}}"
x:Key="checkableListViewItem">
<Setter Property="IsSelected" Value="{Binding Path=IsChecked}" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Top" />
</Style>
The ListView's IsEnabled property does change, if that matters. I wanted to somehow incorporate <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>, or something similar, to highlight selected ListViewItems that are in an unfocused ListView.
I have the following style, but this does not seem to affect ListViewItems in my ListView, and I thought the GridView that I'm using might be the reason. When I tried to override the Template property, the View property got ignored so my GridView wasn't being displayed.
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="{x:Static SystemColors.HighlightColor}"/>
</Style.Resources>
</Style>
How do I highlight in blue the selected items in my ListView when that ListView is unfocused?
This will work for a ListView that doesn't have a GridView
<Style TargetType="{x:Type ListViewItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}"/>
</Style.Resources>
</Style>
Update
1.Without re-templating when using GridView
<LinearGradientBrush x:Key="ListItemSelectedFill" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFD9F4FF" Offset="0"/>
<GradientStop Color="#FF9BDDFB" Offset="1"/>
</LinearGradientBrush>
<Style TargetType="ListViewItem" x:Key="checkableListViewItem">
<Setter Property="IsSelected" Value="{Binding Path=IsChecked}" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource ListItemSelectedFill}" />
<Setter Property="BorderBrush" Value="#FF98DDFB" />
</Trigger>
<!-- Or if you want to choose another color for unfocused
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="Selector.IsSelectionActive" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource ListItemSelectedFill}" />
<Setter Property="BorderBrush" Value="#FF98DDFB" />
</MultiTrigger>
-->
</Style.Triggers>
</Style>
2. Re-templating the ListViewItem when using GridView.
<LinearGradientBrush x:Key="ListItemHoverFill" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF1FBFF" Offset="0"/>
<GradientStop Color="#FFD5F1FE" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="ListItemSelectedFill" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFD9F4FF" Offset="0"/>
<GradientStop Color="#FF9BDDFB" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="ListItemSelectedInactiveFill" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFEEEDED" Offset="0"/>
<GradientStop Color="#FFDDDDDD" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="ListItemSelectedHoverFill" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFEAF9FF" Offset="0"/>
<GradientStop Color="#FFC9EDFD" Offset="1"/>
</LinearGradientBrush>
<Style TargetType="ListViewItem" x:Key="checkableListViewItem">
<Setter Property="IsSelected" Value="{Binding Path=IsChecked}" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border CornerRadius="2" SnapsToDevicePixels="True"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<Border Name="InnerBorder" CornerRadius="1" BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition MaxHeight="11" />
<RowDefinition />
</Grid.RowDefinitions>
<Rectangle Name="UpperHighlight" Visibility="Collapsed" Fill="#75FFFFFF" />
<GridViewRowPresenter Grid.RowSpan="2"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource ListItemHoverFill}" />
<Setter Property="BorderBrush" Value="#FFCCF0FF" />
<Setter TargetName="UpperHighlight" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource ListItemSelectedFill}" />
<Setter Property="BorderBrush" Value="#FF98DDFB" />
<Setter TargetName="InnerBorder" Property="BorderBrush" Value="#80FFFFFF" />
<Setter TargetName="UpperHighlight" Property="Visibility" Value="Visible" />
<Setter TargetName="UpperHighlight" Property="Fill" Value="#40FFFFFF" />
</Trigger>
<!--<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="Selector.IsSelectionActive" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource ListItemSelectedInactiveFill}" />
<Setter Property="BorderBrush" Value="#FFCFCFCF" />
</MultiTrigger>-->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource ListItemSelectedHoverFill}" />
<Setter Property="BorderBrush" Value="#FF98DDFB" />
</MultiTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

What is the style trigger when you are hovering over a button in WPF?

My question is pretty straight forward. I am trying to style the background of a button in WPF when I MouseOver/Hover on the button.
<Style x:Key="StandardButton" TargetType="Button">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="CornflowerBlue" />
</Trigger>
</Style.Triggers>
</Style>
When I MouseOver the button, the background colour briefly changes to CornFlowerBlue, but then changes to the default colours that show when you are remaining over the button for a preiod of time.
What Trigger Property do I need to use to keep the background of the button CornFlowerBlue while I am remaining over the button?
Many Thanks
This the default property of the Button Chrome implmented in the Button. I have removed the Button chrome from the default button template and create my own just see
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
x:Class="WpfApplication9.Window1"
x:Name="Window"
Title="Window1"
Width="640" Height="480">
<Window.Resources>
<Style x:Key="ButtonFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1" Margin="2" SnapsToDevicePixels="true"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#F3F3F3" Offset="0"/>
<GradientStop Color="#EBEBEB" Offset="0.5"/>
<GradientStop Color="#DDDDDD" Offset="0.5"/>
<GradientStop Color="#CDCDCD" Offset="1"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
<Style x:Key="ButtonStyle1" 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}">
<Border Background="{TemplateBinding Background}"
BorderBrush="Black" BorderThickness="2" CornerRadius="5" x:Name="bd">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<Setter TargetName="bd" Property="Background" Value="#FFF40000"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.048*"/>
<ColumnDefinition Width="0.952*"/>
</Grid.ColumnDefinitions>
<Button HorizontalAlignment="Left" Margin="106.048,102,0,0" Style="{DynamicResource ButtonStyle1}" VerticalAlignment="Top" Width="75" Content="Button" Grid.Column="1"/>
</Grid>

Why can't I set the background color of a selected ListBoxItem in WPF?

When a user clicks on an ListBoxItem, I want to it to be
a bold
larger
font red
background yellow
Everything works except the background.
It seems that there is a standard (blue) background for the selected item.
How do I override that and change the selected background yellow?
Here is the code:
<Window x:Class="AlternateListBox2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:local="clr-namespace:AlternateListBox2">
<Window.Resources>
<local:Countries x:Key="countries"/>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Content" Value="{Binding Path=Name}"/>
<Setter Property="Margin" Value="2"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="Background" Value="Yellow"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<ListBox
ItemsSource="{Binding Source={StaticResource countries}}"
Width="100"
Margin="10"
HorizontalAlignment="Left"
/>
</StackPanel>
</Window>
It can be done a lot simpler. The Background color for the selected ListBox items are taken from the SystemColors. So, what you need to do is override the SystemColors in the Resources of your ListBox:
<ListBox.Resources>
<!--Selected color when the ListBox is focused-->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />
<!--Selected color when the ListBox is not focused-->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow" />
</ListBox.Resources>
This code should work for setting background. The problem is that you need to create a ControlTemplate and assign to ContentPresenter's Background property the value "Yellow".
<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="contentPresenter"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="OpacityMask" TargetName="contentPresenter" Value="{x:Null}"/>
<Setter Property="Background" TargetName="Bd" Value="Yellow"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="Selector.IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
"It can be done a lot simpler. The Background color for the selected
ListBox items are taken from the SystemColors. So, what you need to
do is override the SystemColors in the Resources of your ListBox"
The concept of overriding SystemColors, which ListBoxItem template will use for background/foreground is awful and often confuses folks who are new to WPF.
Thus, my recommendation is to override ListBoxItem template and customize it.
Thanks Frances!! That did it for me, well somewhat. Here is my code that allows for the template to use the "StrColor" property for the selected and non-selected list items.
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<!--Nice Brush-->
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<!-- This is a gradient from white to StrColor and back to white -->
<!--<GradientStop Color="White" Offset="0"/>
<GradientStop Color="{Binding Path=StrColor}" Offset="0.445"/>
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="{Binding Path=StrColor}" Offset="0.53"/>-->
<!-- This is a gradient from StrColor to white -->
<GradientStop Color="{Binding Path=StrColor}" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<!--Standard Color-->
<!--<Setter Property="Background" Value="{Binding Path=StrColor}"/>-->
<Setter Property="Foreground" Value="{Binding Path=TextColor}"/>
<Setter Property="Height" Value="{Binding Path=Height}"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="contentPresenter"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="OpacityMask" TargetName="contentPresenter" Value="{x:Null}"/>
<Setter Property="Background" TargetName="Bd">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{Binding Path=StrColor}" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="{Binding Path=TextColor}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>

Resources