I am creating a button with a simple Polyline which I want to change color when the button is Disabled (btnUp.IsEnabled = false).
I tried this:
<RepeatButton x:Name="btnUp" Foreground="Green">
<RepeatButton.Style>
<Style TargetType="{x:Type RepeatButton}">
<Setter Property="Content">
<Setter.Value>
<Polyline x:Name="arrowUp" Points="0,2 3,0 6,2" Stroke="{Binding RelativeSource={RelativeSource AncestorType=RepeatButton}, Path=Foreground, Mode=TwoWay}" StrokeThickness="2"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#FFFFFFFF"/>
</Trigger>
</Style.Triggers>
</Style>
</RepeatButton.Style>
</RepeatButton>
But the Polyline still has the same (Green) color when the button is disabled. Even though I did expect it to be white because of the databinding between the button.foreground and polyline.stroke.
However if I change the trigger to this it works (the button is collapsed):
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
I found the answer on my own.
I moved the Foreground="Green" to a separate setter, <Setter Property="Foreground" Value="#FF383838"/> inside the <Style>, then it worked.
I'm not sure why though.
Here's the complete solution:
<RepeatButton x:Name="btnUp">
<RepeatButton.Style>
<Style TargetType="{x:Type RepeatButton}">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="Content">
<Setter.Value>
<Polyline x:Name="arrowUp" Points="0,2 3,0 6,2" Stroke="{Binding RelativeSource={RelativeSource AncestorType=RepeatButton}, Path=Foreground, Mode=TwoWay}" StrokeThickness="2"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#FFFFFFFF"/>
</Trigger>
</Style.Triggers>
</Style>
</RepeatButton.Style>
</RepeatButton>
Related
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>
I have a DataGrid in which I can change the respective colors when my CheckBox is activated. However, my IsMouseOver event only works if my CheckBox is true. As soon as my CheckBox is set to false, my IsMouseOver Effect no longer works, why? Do I have to set a trigger somewhere?
My Code from my DataGrid:
<DataGrid x:Name="datagrid" ItemsSource="{Binding}"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserResizeRows="True" GridLinesVisibility="None"
ColumnWidth="*" DockPanel.Dock="Bottom"
Background="#222831" Foreground="White"
AutoGenerateColumns="True">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Command="Copy"/>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.Resources>
<!--Design kopfzeile-->
<Style TargetType="{x:Type DataGridColumnHeader}" x:Name="test" >
<Setter Property="Background" Value="#292F3B"/>
<Setter Property="Foreground" Value="LightBlue"/>
<Setter Property="FontWeight" Value="SemiBold"/>
<Setter Property="Height" Value="30"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="BorderThickness" Value="0,0,2,0" />
<Setter Property="BorderBrush" Value="#333333"/>
<Setter Property="Padding" Value="10 0 0 0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Border x:Name="insideHeader" Background="#242A36">
<Border x:Name="borderHeader" BorderThickness="1"
CornerRadius="6"
Background="#2D2D30"
Padding="10,0,0,0"
Margin="2">
<ContentPresenter/>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="borderHeader" Property="Background" Value="#4182C6"/>
</Trigger>
<DataTrigger Binding="{Binding ElementName=toogleButton,Path=IsChecked}" Value="False">
<Setter TargetName="borderHeader" Property="Background" Value="#FA9F34"/>
<Setter Property="Foreground" Value="#2B2B2B"/>
<Setter TargetName="insideHeader" Property="Background" Value="#00336E"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--Deaktivieren Des rowheader-->
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Background" Value="Transparent"/>
</Style>
<!--Cellen Design-->
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="#292F3B"/>
<Setter Property="Foreground" Value="LightBlue"/>
<Setter Property="BorderThickness" Value="0,0,2,0" />
<Setter Property="BorderBrush" Value="#333333"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="insideBorder" Background="#242A36">
<Border x:Name="BorderCell" BorderThickness="1"
CornerRadius="6"
Background="#292F3B"
Padding="10,0,0,0"
Margin="2">
<ContentPresenter/>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="BorderCell" Property="Background" Value="#4182C6"/>
</Trigger>
<DataTrigger Binding="{Binding ElementName=toogleButton,Path=IsChecked}" Value="False">
<Setter TargetName="BorderCell" Property="Background" Value="#0051B0"/>
<Setter TargetName="insideBorder" Property="Background" Value="#00336E"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
</DataGrid>
change triggers' order. they both set Background. at the moment DataTrigger for IsChecked=false overrides Trigger for IsMouseOver=true, when CheckBox is unchecked, because DataTrigger is applied last.
<DataTrigger Binding="{Binding ElementName=toogleButton,Path=IsChecked}" Value="False">
<Setter TargetName="borderHeader" Property="Background" Value="#FA9F34"/>
<Setter Property="Foreground" Value="#2B2B2B"/>
<Setter TargetName="insideHeader" Property="Background" Value="#00336E"/>
</DataTrigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="borderHeader" Property="Background" Value="#4182C6"/>
</Trigger>
We use a custom image for buttons in our project. We apply this via a style but now have a need to 'disable' the buttons and are trying to swap out the image for a new one.
The first Trigger works 100% as expected. When the button's IsEnabled == True, the button is 'green'. When the button's IsEnabled == False, the Green is removed and the Foreground color changes BUT the Grey image will not load up.
Am I missing something???
<Style TargetType="{x:Type Button}" x:Key="GreenGreyButton">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="..\Images\UI\Buttons\green.png"/>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#FFA30046"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="..\Images\UI\Buttons\grey.png"/>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Gray"/>
</Trigger>
</Style.Triggers>
</Style>
It's a pity that when the IsEnabled is false, the native style will override all settings (with the default gray background). You can however re-template the button like this:
<Style TargetType="{x:Type Button}" x:Key="GreenGreyButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}">
<ContentPresenter Content="{TemplateBinding Content}"
Margin="{TempateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="..\Images\UI\Buttons\green.png"/>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#FFA30046"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="..\Images\UI\Buttons\grey.png"/>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Gray"/>
</Trigger>
</Style.Triggers>
</Style>
Add the image as content to the button & use DatatTrigger instead, like this:
<Button...
>
<StackPanel>
<Image>
<Image.Style>
<Style TargetType="Image">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="True">
<Setter Property="Source" Value="..\Images\UI\Buttons\green.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="False">
<Setter Property="Source" Value="..\Images\UI\Buttons\grey.png"/>
<Setter Property="Foreground" Value="Gray"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</StackPanel>
</Button>
You need to set default values for your properties
<Style TargetType="{x:Type Button}" x:Key="GreenGreyButton">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="..\Images\UI\Buttons\green.png"/>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#FFA30046"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="..\Images\UI\Buttons\grey.png"/>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Gray"/>
</Trigger>
</Style.Triggers>
I have problem with ListBox item style, I create two styles and do not know to use it together. 1st style is for ListBox item size, mouse over color and so on, or second is for item background (Alternation count). If I leave one of them they work fine, but how to make them work together? Or maybe I could it write in one style?
My code is:
..... <Style x:Key="Style2"
TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border
Name="Border"
Padding="7"
SnapsToDevicePixels="True">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background"
Value="{StaticResource SelectedBackgroundBrush}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground"
Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#FFFFFF"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#F7F7F7"></Setter>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="{x:Type ListBoxItem}"
TargetType="{x:Type ListBoxItem}"
BasedOn="{StaticResource Style2}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#19f39611"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#19000000"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid >
<ScrollViewer Margin="30,98,362,30">
<ListBox x:Name="lbPersonList" AlternationCount="2">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Grid>
You can use BasedOn
<Style x:Key="Style1" TargetType="ListBoxItem">
...
</Style>
<Style x:Key="{x:Type ListBoxItem}" TargetType="ListBoxItem" BasedOn={StaticResource Style1}>
...
</Style>
EDITED
The problem was the Background setter of the ControlTemplate. This is the solution (using AlternationConverter instead of triggers):
<Window.Resources>
<AlternationConverter x:Key="BackgroundConverter">
<SolidColorBrush Color="#19f39611" />
<SolidColorBrush Color="#19000000" />
</AlternationConverter>
<Style x:Key="Style2" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="7" SnapsToDevicePixels="True" Background="{TemplateBinding Background}">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="Gray"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="Green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="Style1" TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource Style2}">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self},
Path=(ItemsControl.AlternationIndex),
Converter={StaticResource BackgroundConverter}}"/>
</Style>
</Window.Resources>
<ListBox x:Name="lbPersonList" AlternationCount="2" ItemContainerStyle="{StaticResource Style1}">
...
Using Dynamic resource you can achieve this using single listboxitem style
<Window.Resources>
<Style x:Key="Lststyle" TargetType="ListBoxItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Background="Transparent" Padding="7" SnapsToDevicePixels="True">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ListBox.AlternationIndex" Value="0">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Color0}"/>
</Trigger>
<Trigger Property="ListBox.AlternationIndex" Value="1">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Color1}"/>
</Trigger>
<Trigger Property="ListBoxItem.IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="Green"/>
</Trigger>
<Trigger Property="ListBoxItem.IsEnabled" Value="false">
<Setter Property="Foreground" Value="LightGray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel >
<TextBlock Text="Listbox1"></TextBlock>
<ScrollViewer >
<ListBox x:Name="lbPersonList" ItemContainerStyle="{StaticResource Lststyle}" AlternationCount="2">
<ListBox.Resources>
<SolidColorBrush x:Key="Color0" Color="#19f39611"></SolidColorBrush>
<SolidColorBrush x:Key="Color1" Color="#19000000"></SolidColorBrush>
</ListBox.Resources>
<TextBlock Text="listboxitem1"></TextBlock>
<TextBlock Text="listboxitem1"></TextBlock>
<TextBlock Text="listboxitem1"></TextBlock>
</ListBox>
</ScrollViewer>
<TextBlock Margin="0,10,0,0" Text="Listbox2"></TextBlock>
<ScrollViewer>
<ListBox x:Name="lbPersonList1" ItemContainerStyle="{StaticResource Lststyle}" AlternationCount="2">
<ListBox.Resources>
<SolidColorBrush x:Key="Color0" Color="Yellow"></SolidColorBrush>
<SolidColorBrush x:Key="Color1" Color="Blue"></SolidColorBrush>
</ListBox.Resources>
<TextBlock Text="listboxitem1"></TextBlock>
<TextBlock Text="listboxitem1"></TextBlock>
<TextBlock Text="listboxitem1"></TextBlock>
</ListBox>
</ScrollViewer>
</StackPanel>
Simplified xaml
<Window.Resources>
<Style x:Key="lst1" TargetType="ListBox" >
<Style.Resources>
<SolidColorBrush x:Key="Color0" Color="#19f39611"></SolidColorBrush>
<SolidColorBrush x:Key="Color1" Color="#19000000"></SolidColorBrush>
</Style.Resources>
</Style>
<Style x:Key="lst2" TargetType="ListBox" >
<Style.Resources>
<SolidColorBrush x:Key="Color0" Color="Blue"></SolidColorBrush>
<SolidColorBrush x:Key="Color1" Color="Yellow"></SolidColorBrush>
</Style.Resources>
</Style>
<Style x:Key="Lststyle" TargetType="ListBoxItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Background="Transparent" Padding="7" SnapsToDevicePixels="True">
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="{DynamicResource Color0}"/>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="{DynamicResource Color1}"/>
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ListBox.AlternationIndex" Value="0">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Color0}"/>
</Trigger>
<Trigger Property="ListBox.AlternationIndex" Value="1">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource Color1}"/>
</Trigger>
<Trigger Property="ListBoxItem.IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="Green"/>
</Trigger>
<Trigger Property="ListBoxItem.IsEnabled" Value="false">
<Setter Property="Foreground" Value="LightGray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel >
<TextBlock Text="Listbox1"></TextBlock>
<ScrollViewer >
<ListBox x:Name="lbPersonList" Style="{StaticResource lst1}" ItemContainerStyle="{StaticResource Lststyle}" AlternationCount="2">![enter image description here][2]
<TextBlock Text="listboxitem1"></TextBlock>
<TextBlock Text="listboxitem1"></TextBlock>
<TextBlock Text="listboxitem1"></TextBlock>
</ListBox>
</ScrollViewer>
<TextBlock Margin="0,10,0,0" Text="Listbox2"></TextBlock>
<ScrollViewer>
<ListBox x:Name="lbPersonList1" Style="{StaticResource lst2}" ItemContainerStyle="{StaticResource Lststyle}" AlternationCount="2">
<TextBlock Text="listboxitem1"></TextBlock>
<TextBlock Text="listboxitem1"></TextBlock>
<TextBlock Text="listboxitem1"></TextBlock>
</ListBox>
</ScrollViewer>
</StackPanel>
You should set for each style a proper x:Key and then for one of your style you can use BasedOn={StaticResource Style1} which appends to your current style, style1. (Check documentation: https://msdn.microsoft.com/en-us/library/system.windows.style.basedon(v=vs.110).aspx)
Check this one:
<Style x:Key="Style2"
TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex"
Value="0">
<Setter Property="Background"
Value="#19f39611"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex"
Value="1">
<Setter Property="Background"
Value="#19000000"></Setter>
</Trigger>
</Style.Triggers>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border"
Padding="7"
SnapsToDevicePixels="True">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected"
Value="true">
<Setter Property="Background"
Value="Red" />
</Trigger>
<Trigger Property="IsEnabled"
Value="false">
<Setter Property="Foreground"
Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type ListBoxItem}"
BasedOn="{StaticResource Style2}">
<Setter Property="SnapsToDevicePixels"
Value="true" />
<Style.Triggers>
<Trigger Property="ListBox.AlternationIndex"
Value="0">
<Setter Property="Background"
Value="CornflowerBlue" />
<Setter Property="Foreground"
Value="Black" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex"
Value="1">
<Setter Property="Background"
Value="LightBlue" />
<Setter Property="Foreground"
Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
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>