I try to change button's style when it is disabled:
<Style TargetType="Button" x:Key="MyButton2">
<Setter Property="Background" Value="MediumAquamarine" />
<Setter Property="Foreground" Value="MediumBlue" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" Value="Green"/>
<Setter Property="Foreground" Value="DeepPink"/>
</Trigger>
</Style.Triggers>
</Style>
and my button:
<Button Style="{StaticResource MyButton2}" Content="My text" Width="100" Height="30" IsEnabled="False" />
But for some reason This style is not applied to the button:
How can i apply this style to my button? And can i do it without overriding button's template only with styles?
I think not, because the Controls in the Windows has a default
Styles
andControlTemplates
and in each version of Windows they are different. In addition, the styles - it's just a lot of settings, usually the style does not change/add behavior to control, which is responsible for thisControlTemplate
.Note:
the Style it is a set of setters, for example: set the background, size, etc. TheControlTemplate
is the form, in which all of these settings will appear.In this situation, I recommend you all to change the ControlTemplate, where it is possible to have the same behavior aka view, regardless of the version of the system.
In this case, try this
Style
:The odd thing is that it "works" when i run it, well partly, and this is likely to the template of the button actually. Here is a buttons entire template.
As you can see there is alot of things going on in the control template of the button. In example the following SolidColorBrushes are used when the ctl is disabled.
You can either change these (in scope of your usercontrol, app), or you can rewrite the control template as Suggested by Daniel Ward.
You may dump the default template of any control by right clicking it in the UI Editor, Edit Template -> Create a copy... Through blend, code or disassembling :)
Hope it helps!
Cheers
Stian
Might be late on this one but there is an in built property provided by WPF:
IsHitTestVisible="False"
Any kind of mouse movement is not detectable using the above on a control thus disabling a control without changing its actual look and feel.
It seems that the only way to do this is to remove the chrome theme from the button. Note that this will remove the hovering/clicking animations, as well as the button's gradient on Windows 7, if you're building for that.
Unfortunately, Microsoft decided that disabling a button should give the background a hardcoded value from a private property in the chrome theme's code-behind, which means that you can't change it through the XAML. That link describes the problem:
The control template for
Button
usesButtonChrome
, which has a private property calledButtonOverlay
, which, when theButton
hasIsEnabled
set toFalse
, sets theBackground
tonew SolidColorBrush(Color.FromRgb(0xf4, 0xf4, 0xf4))
(or RGB 244,244,244), which is the grey you see. You can't change that.What you can do is create a new control template for the button (yay), replacing the
Themes:ButtonChrome
element with aBorder
element, which you are able to change theBackground
of.It may not appear to work in design view, but it works when you run it.
So, for instance, for yours, it might look something like:
My solution is a bit easier but it does not allow to use a
Transparent
background color for a disabled button and you cannot change theBorderBrush
either. What I do is using aTextBlock
as a content of the button and change the color of thisTextBlock
background when it's disabled.My button is written like this:
And MyButtonContentStyle is written like this:
It worked like a charm for us.