0

I did a lot of search on this and end up no answer.

following are some of my search results:

Setting combo box foreground color for the disabled state

WPF combobox disabled background color

WPF ComboBox: background color when disabled

some people said it works, but some said no. and i tried, and i am with the first kind.

so how exactly to make it work? i searched msdn, and the answers are to copy and paste standard template then modify it.

sure it will work, but i just need to change the color. it's too complicated for a peanut.

i made myself a fast self contained xaml only program, and in my box, it doesn't work:

<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox Margin="0,0,170,253">
            <ComboBox.Style>
                <Style TargetType="ComboBox">
                    <Setter Property="IsEnabled" Value="{Binding IsChecked, ElementName=tgb}"/>
                    <Style.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" Value="#FF000000"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="true">
                            <Setter Property="Background" Value="#FF474747"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ComboBox.Style>
        </ComboBox>
        <ToggleButton x:Name="tgb" Margin="347,218,10,10"/>
    </Grid>
</Window>

IsEnabled toggle as the toggle button gets clicked, and when it's disabled, the combobox should be black, while it's not.

the most confusing part is, the trigger on IsEnabled is true works but the one on false does not.

could somebody show me how to fix that?

i am now using .net 4.5.1, visual studio 2013.

Community
  • 1
  • 1
Jason Hu
  • 6,239
  • 1
  • 20
  • 41

1 Answers1

0

The problem is, that the default template will make a rectangle visible (with opacity 0.55) which lies over the control when the VisualState changes to Disabled.

<Rectangle x:Name="DisabledVisualElement" RadiusX="3" RadiusY="3" Fill="White" Opacity="0" IsHitTestVisible="false" />

<vsm:VisualState x:Name="Disabled">
  <Storyboard>
    <DoubleAnimation Duration="00:00:00" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)" To=".55"/>
  </Storyboard>
</vsm:VisualState>

thats why the trigger in your style has no effect. Here is th link to the default template: ComboBox template

The best way is (as you said) to override the complete template with your own. You can copy the default template into your project and change the code i posted above.

Manusch
  • 31
  • 3
  • thanks. but why somebody would say it works? as i said, modifying the template is what i want to avoid. too much code for too little thing. if it has to be done in this way, i would like to leave it there. – Jason Hu Feb 25 '15 at 14:20
  • i dont know why somebody said this. But there is no other way to remove the overlayed Rectangle. you can simply copy&paste the default template and remove the code i posted above, this is not that much work. Create an extra ResourceDictionary with the template and merge it with the rest within the App.Resources so that your working code dont get messed up. I think that is an easy solution – Manusch Feb 25 '15 at 16:17