Button Change Background Color on Click / Press











up vote
0
down vote

favorite












I have what I thought would be an easy issue to resolve but for the life of me I cant get exactly what i want using WPF (coming from a WinForms background).



All I am trying to do is create a button which the background color changes just while the button is pressed / clicked. Once the button is released, it reverts back to normal. Based on this I am trying to also have nothing happen on Mouse Hover.



Ive managed to create a template to remove the Mouse Over effects but can seem to figure out how to change the background color just whilst clicked / pressed and then reset.



I have been working off this template from another Stack Over flow post which gives me a nice transition of the button being pressed.



I am happy to lose the effect but was using this as a basis for trying to understand how to piece it all together.



 <Style x:Key="InformButton" TargetType="{x:Type Button}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="10px"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource MyFocusVisual}" />
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop Color="#FFFFD190" Offset="0.2"/>
<GradientStop Color="Orange" Offset="0.85"/>
<GradientStop Color="#FFFFD190" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border"
BorderThickness="1"
Padding="4,2"
BorderBrush="DarkGray"
CornerRadius="3"
Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="contentShadow"
Style="{StaticResource ShadowStyle}" >
<ContentPresenter.RenderTransform>
<TranslateTransform X="1.0" Y="1.0" />
</ContentPresenter.RenderTransform>
</ContentPresenter>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="content" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="content" Property="RenderTransform" >
<Setter.Value>
<TranslateTransform Y="2.0" />
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#007DB8" />
</Trigger>
<Trigger Property="IsDefaulted" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Opacity" Value="0.7" />
<Setter Property="Foreground" Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>









share|improve this question




















  • 1




    Can you show us an example of what you have tried so far?
    – John
    Nov 12 at 6:12















up vote
0
down vote

favorite












I have what I thought would be an easy issue to resolve but for the life of me I cant get exactly what i want using WPF (coming from a WinForms background).



All I am trying to do is create a button which the background color changes just while the button is pressed / clicked. Once the button is released, it reverts back to normal. Based on this I am trying to also have nothing happen on Mouse Hover.



Ive managed to create a template to remove the Mouse Over effects but can seem to figure out how to change the background color just whilst clicked / pressed and then reset.



I have been working off this template from another Stack Over flow post which gives me a nice transition of the button being pressed.



I am happy to lose the effect but was using this as a basis for trying to understand how to piece it all together.



 <Style x:Key="InformButton" TargetType="{x:Type Button}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="10px"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource MyFocusVisual}" />
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop Color="#FFFFD190" Offset="0.2"/>
<GradientStop Color="Orange" Offset="0.85"/>
<GradientStop Color="#FFFFD190" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border"
BorderThickness="1"
Padding="4,2"
BorderBrush="DarkGray"
CornerRadius="3"
Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="contentShadow"
Style="{StaticResource ShadowStyle}" >
<ContentPresenter.RenderTransform>
<TranslateTransform X="1.0" Y="1.0" />
</ContentPresenter.RenderTransform>
</ContentPresenter>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="content" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="content" Property="RenderTransform" >
<Setter.Value>
<TranslateTransform Y="2.0" />
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#007DB8" />
</Trigger>
<Trigger Property="IsDefaulted" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Opacity" Value="0.7" />
<Setter Property="Foreground" Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>









share|improve this question




















  • 1




    Can you show us an example of what you have tried so far?
    – John
    Nov 12 at 6:12













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have what I thought would be an easy issue to resolve but for the life of me I cant get exactly what i want using WPF (coming from a WinForms background).



All I am trying to do is create a button which the background color changes just while the button is pressed / clicked. Once the button is released, it reverts back to normal. Based on this I am trying to also have nothing happen on Mouse Hover.



Ive managed to create a template to remove the Mouse Over effects but can seem to figure out how to change the background color just whilst clicked / pressed and then reset.



I have been working off this template from another Stack Over flow post which gives me a nice transition of the button being pressed.



I am happy to lose the effect but was using this as a basis for trying to understand how to piece it all together.



 <Style x:Key="InformButton" TargetType="{x:Type Button}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="10px"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource MyFocusVisual}" />
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop Color="#FFFFD190" Offset="0.2"/>
<GradientStop Color="Orange" Offset="0.85"/>
<GradientStop Color="#FFFFD190" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border"
BorderThickness="1"
Padding="4,2"
BorderBrush="DarkGray"
CornerRadius="3"
Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="contentShadow"
Style="{StaticResource ShadowStyle}" >
<ContentPresenter.RenderTransform>
<TranslateTransform X="1.0" Y="1.0" />
</ContentPresenter.RenderTransform>
</ContentPresenter>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="content" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="content" Property="RenderTransform" >
<Setter.Value>
<TranslateTransform Y="2.0" />
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#007DB8" />
</Trigger>
<Trigger Property="IsDefaulted" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Opacity" Value="0.7" />
<Setter Property="Foreground" Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>









share|improve this question















I have what I thought would be an easy issue to resolve but for the life of me I cant get exactly what i want using WPF (coming from a WinForms background).



All I am trying to do is create a button which the background color changes just while the button is pressed / clicked. Once the button is released, it reverts back to normal. Based on this I am trying to also have nothing happen on Mouse Hover.



Ive managed to create a template to remove the Mouse Over effects but can seem to figure out how to change the background color just whilst clicked / pressed and then reset.



I have been working off this template from another Stack Over flow post which gives me a nice transition of the button being pressed.



I am happy to lose the effect but was using this as a basis for trying to understand how to piece it all together.



 <Style x:Key="InformButton" TargetType="{x:Type Button}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="10px"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource MyFocusVisual}" />
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop Color="#FFFFD190" Offset="0.2"/>
<GradientStop Color="Orange" Offset="0.85"/>
<GradientStop Color="#FFFFD190" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border"
BorderThickness="1"
Padding="4,2"
BorderBrush="DarkGray"
CornerRadius="3"
Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="contentShadow"
Style="{StaticResource ShadowStyle}" >
<ContentPresenter.RenderTransform>
<TranslateTransform X="1.0" Y="1.0" />
</ContentPresenter.RenderTransform>
</ContentPresenter>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="content" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="content" Property="RenderTransform" >
<Setter.Value>
<TranslateTransform Y="2.0" />
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#007DB8" />
</Trigger>
<Trigger Property="IsDefaulted" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Opacity" Value="0.7" />
<Setter Property="Foreground" Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>






c# wpf button






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 6:19

























asked Nov 12 at 6:11









Daniel Kelly

114




114








  • 1




    Can you show us an example of what you have tried so far?
    – John
    Nov 12 at 6:12














  • 1




    Can you show us an example of what you have tried so far?
    – John
    Nov 12 at 6:12








1




1




Can you show us an example of what you have tried so far?
– John
Nov 12 at 6:12




Can you show us an example of what you have tried so far?
– John
Nov 12 at 6:12












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










The answer is actually pretty simple - you already have a "Is Pressed" trigger - you just need to add a background setter there, for example:



<Setter Property="Background" Value="Red"/>


To make it simple to understand, we can see a simplified version of your template:



<Style x:Key="InformButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="LightBlue">
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" BorderThickness="1" Padding="4,2"
BorderBrush="DarkGray" CornerRadius="3"
Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="content" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


Here you can see that all we have is a basic background (Light blue), a content presenter for the content and a trigger that if the button is pressed, will change the color to red.



Personally, I think that this minimal template also looks better as it will match the global font settings and style.






share|improve this answer





















  • Thanks, that works exactly as expected. What if I wanted to override / change the border of the button on press event only?
    – Daniel Kelly
    Nov 16 at 19:53










  • Similarly. First you should find the property on the Button that you want to change, in this case BorderBrush. Then you need to add to the trigger a setter for this property, e.g. <Setter Property="BorderBrush" Value="Blue"/>. This will not work immediately - because the template is not using this property from the button. To fix this you need to add a setter to the style itself (like the background): <Setter Property="Background" Value="DarkGray"/> and instead of hard coding to the border in the template, use a TemplateBinding: BorderBrush="{TemplateBinding BorderBrush}".
    – Alon Kashtan
    Nov 18 at 7:18












  • I would suggest you to read more about templating in WPF, it is not difficult and very powerful.
    – Alon Kashtan
    Nov 18 at 7:22











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256719%2fbutton-change-background-color-on-click-press%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










The answer is actually pretty simple - you already have a "Is Pressed" trigger - you just need to add a background setter there, for example:



<Setter Property="Background" Value="Red"/>


To make it simple to understand, we can see a simplified version of your template:



<Style x:Key="InformButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="LightBlue">
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" BorderThickness="1" Padding="4,2"
BorderBrush="DarkGray" CornerRadius="3"
Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="content" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


Here you can see that all we have is a basic background (Light blue), a content presenter for the content and a trigger that if the button is pressed, will change the color to red.



Personally, I think that this minimal template also looks better as it will match the global font settings and style.






share|improve this answer





















  • Thanks, that works exactly as expected. What if I wanted to override / change the border of the button on press event only?
    – Daniel Kelly
    Nov 16 at 19:53










  • Similarly. First you should find the property on the Button that you want to change, in this case BorderBrush. Then you need to add to the trigger a setter for this property, e.g. <Setter Property="BorderBrush" Value="Blue"/>. This will not work immediately - because the template is not using this property from the button. To fix this you need to add a setter to the style itself (like the background): <Setter Property="Background" Value="DarkGray"/> and instead of hard coding to the border in the template, use a TemplateBinding: BorderBrush="{TemplateBinding BorderBrush}".
    – Alon Kashtan
    Nov 18 at 7:18












  • I would suggest you to read more about templating in WPF, it is not difficult and very powerful.
    – Alon Kashtan
    Nov 18 at 7:22















up vote
0
down vote



accepted










The answer is actually pretty simple - you already have a "Is Pressed" trigger - you just need to add a background setter there, for example:



<Setter Property="Background" Value="Red"/>


To make it simple to understand, we can see a simplified version of your template:



<Style x:Key="InformButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="LightBlue">
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" BorderThickness="1" Padding="4,2"
BorderBrush="DarkGray" CornerRadius="3"
Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="content" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


Here you can see that all we have is a basic background (Light blue), a content presenter for the content and a trigger that if the button is pressed, will change the color to red.



Personally, I think that this minimal template also looks better as it will match the global font settings and style.






share|improve this answer





















  • Thanks, that works exactly as expected. What if I wanted to override / change the border of the button on press event only?
    – Daniel Kelly
    Nov 16 at 19:53










  • Similarly. First you should find the property on the Button that you want to change, in this case BorderBrush. Then you need to add to the trigger a setter for this property, e.g. <Setter Property="BorderBrush" Value="Blue"/>. This will not work immediately - because the template is not using this property from the button. To fix this you need to add a setter to the style itself (like the background): <Setter Property="Background" Value="DarkGray"/> and instead of hard coding to the border in the template, use a TemplateBinding: BorderBrush="{TemplateBinding BorderBrush}".
    – Alon Kashtan
    Nov 18 at 7:18












  • I would suggest you to read more about templating in WPF, it is not difficult and very powerful.
    – Alon Kashtan
    Nov 18 at 7:22













up vote
0
down vote



accepted







up vote
0
down vote



accepted






The answer is actually pretty simple - you already have a "Is Pressed" trigger - you just need to add a background setter there, for example:



<Setter Property="Background" Value="Red"/>


To make it simple to understand, we can see a simplified version of your template:



<Style x:Key="InformButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="LightBlue">
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" BorderThickness="1" Padding="4,2"
BorderBrush="DarkGray" CornerRadius="3"
Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="content" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


Here you can see that all we have is a basic background (Light blue), a content presenter for the content and a trigger that if the button is pressed, will change the color to red.



Personally, I think that this minimal template also looks better as it will match the global font settings and style.






share|improve this answer












The answer is actually pretty simple - you already have a "Is Pressed" trigger - you just need to add a background setter there, for example:



<Setter Property="Background" Value="Red"/>


To make it simple to understand, we can see a simplified version of your template:



<Style x:Key="InformButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="LightBlue">
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" BorderThickness="1" Padding="4,2"
BorderBrush="DarkGray" CornerRadius="3"
Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center" x:Name="content" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


Here you can see that all we have is a basic background (Light blue), a content presenter for the content and a trigger that if the button is pressed, will change the color to red.



Personally, I think that this minimal template also looks better as it will match the global font settings and style.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 7:14









Alon Kashtan

563




563












  • Thanks, that works exactly as expected. What if I wanted to override / change the border of the button on press event only?
    – Daniel Kelly
    Nov 16 at 19:53










  • Similarly. First you should find the property on the Button that you want to change, in this case BorderBrush. Then you need to add to the trigger a setter for this property, e.g. <Setter Property="BorderBrush" Value="Blue"/>. This will not work immediately - because the template is not using this property from the button. To fix this you need to add a setter to the style itself (like the background): <Setter Property="Background" Value="DarkGray"/> and instead of hard coding to the border in the template, use a TemplateBinding: BorderBrush="{TemplateBinding BorderBrush}".
    – Alon Kashtan
    Nov 18 at 7:18












  • I would suggest you to read more about templating in WPF, it is not difficult and very powerful.
    – Alon Kashtan
    Nov 18 at 7:22


















  • Thanks, that works exactly as expected. What if I wanted to override / change the border of the button on press event only?
    – Daniel Kelly
    Nov 16 at 19:53










  • Similarly. First you should find the property on the Button that you want to change, in this case BorderBrush. Then you need to add to the trigger a setter for this property, e.g. <Setter Property="BorderBrush" Value="Blue"/>. This will not work immediately - because the template is not using this property from the button. To fix this you need to add a setter to the style itself (like the background): <Setter Property="Background" Value="DarkGray"/> and instead of hard coding to the border in the template, use a TemplateBinding: BorderBrush="{TemplateBinding BorderBrush}".
    – Alon Kashtan
    Nov 18 at 7:18












  • I would suggest you to read more about templating in WPF, it is not difficult and very powerful.
    – Alon Kashtan
    Nov 18 at 7:22
















Thanks, that works exactly as expected. What if I wanted to override / change the border of the button on press event only?
– Daniel Kelly
Nov 16 at 19:53




Thanks, that works exactly as expected. What if I wanted to override / change the border of the button on press event only?
– Daniel Kelly
Nov 16 at 19:53












Similarly. First you should find the property on the Button that you want to change, in this case BorderBrush. Then you need to add to the trigger a setter for this property, e.g. <Setter Property="BorderBrush" Value="Blue"/>. This will not work immediately - because the template is not using this property from the button. To fix this you need to add a setter to the style itself (like the background): <Setter Property="Background" Value="DarkGray"/> and instead of hard coding to the border in the template, use a TemplateBinding: BorderBrush="{TemplateBinding BorderBrush}".
– Alon Kashtan
Nov 18 at 7:18






Similarly. First you should find the property on the Button that you want to change, in this case BorderBrush. Then you need to add to the trigger a setter for this property, e.g. <Setter Property="BorderBrush" Value="Blue"/>. This will not work immediately - because the template is not using this property from the button. To fix this you need to add a setter to the style itself (like the background): <Setter Property="Background" Value="DarkGray"/> and instead of hard coding to the border in the template, use a TemplateBinding: BorderBrush="{TemplateBinding BorderBrush}".
– Alon Kashtan
Nov 18 at 7:18














I would suggest you to read more about templating in WPF, it is not difficult and very powerful.
– Alon Kashtan
Nov 18 at 7:22




I would suggest you to read more about templating in WPF, it is not difficult and very powerful.
– Alon Kashtan
Nov 18 at 7:22


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256719%2fbutton-change-background-color-on-click-press%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)