2

I want to Avoid the Color Change of a Button when it gets disabled. The Button Color should be the Same if its diabled or not.

Using a style I can change the Background Color when it gets disabled:

<Style TargetType="{x:Type Button}">
...
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Button}">
        ...
        <ControlTemplate.Triggers>
         ...
          <Trigger Property="IsEnabled" Value="false">
            <Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
            <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
            <Setter Property="Foreground" Value="#888888"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

I can change the Background color there, but I want to keep it dynamic because the Background Color is databound and should not change.

If I delete the Background setter, the default background color change is performed.

How can I disable the colorchange? Or at least make the Disabled-Background-Color databound?

Sorry for my bad english.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Doc Snuggles
  • 241
  • 1
  • 3
  • 15

2 Answers2

0

If you want to make it data-bonund most appropriate place, imo, is declaring it inside relative DataTmplate of your control, where you specify the Style (already defined by you) and data applied to the control.

There you can define a Converter between some your state and relative color you would like to appear on the button.

OR

If you would like to limit yourself in Style, you can define a databinding inside Style itself. Imo not very logical, but it is possible.

Have a look on this answer:

Change Button Background color through MVVM pattern in WPF

Community
  • 1
  • 1
Tigran
  • 61,654
  • 8
  • 86
  • 123
  • I dont´t get how I can change the Background_Color in the Disabled-State of the button via Style or datatemplate. Can you specify it please? – Doc Snuggles Apr 09 '13 at 07:34
  • @DocSnuggles: did you look on link provided in my answer? There is an actual example of how to do that. – Tigran Apr 09 '13 at 07:37
0

I know this is late but hopefully it helps someone. This is how I would setup the style:

<Style TargetType="Button">
...
  <Style.Triggers>
     <Trigger TargetType="Button" Property="IsEnabled" Value="false">
       <Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
       <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
       <Setter Property="Foreground" Value="#888888"/>
     </Trigger>       
  </Style.Triggers>
</Style>

You need to include the TargetType again in the Trigger or else you'll get an exception.