JustMeToo
JustMeToo

Reputation: 425

wpf comboBox coloring issue

Due to requirements, I need to have a comboBox that works as follows:

It uses: - one set of colors for fore/background when in view mode - A second set of colors for fore/background when in edit mode - Another set for selected mode (when the cursor is in the comboBox) - Another set for disabled mode

The user will never be able to edit the contents, just click on the down arrow and select from the list.

I have the comboBox working except for the colors. Unlike other controls, simply trying to do the following (the triggers for edit mode) just doesn't work:

<MultiTrigger>
<MultiTrigger.Conditions>
    <Condition Property="IsFocused"
         Value="false" />
    <Condition Property="wpfMisc:myCtrl.viewMode"
         Value="false" />
    <Condition Property="IsEnabled"
         Value="true" />
</MultiTrigger.Conditions>
<Setter Property="BorderBrush"
    Value="{DynamicResource controls-editableBorderBrush}" />
<Setter Property="Background"
    Value="{DynamicResource controls-editableBackgroundBrush}" />
<Setter Property="Foreground"
    Value="{DynamicResource controls-editableForegroundBrush}" />
</MultiTrigger>

What do I need to set in my style so that I can change the fore/back color of the displayed SelectedItem - i.e. make the above work?

And I am curious if anyone can tell me why a control like this doesn't use a similar interface as other data entry controls (isn't that the whole idea of polymorphism?) This isn't a big deal, just curious, that's all.

Thanks!

Upvotes: 0

Views: 443

Answers (1)

Sheridan
Sheridan

Reputation: 69959

You don't achieve what you're after in the way that you are currently trying to achieve it. There is no need to use a MultiTrigger, just a number of sequential Trigger objects:

<ComboBox Width="150" Height="24">
    <ComboBox.Style>
        <Style>
            <Setter Property="ComboBox.Background" Value="Green" />
            <Style.Triggers>
                <Trigger Property="ComboBox.IsFocused" Value="True">
                    <Setter Property="ComboBox.Background" Value="Red" />
                </Trigger>
                <Trigger Property="ComboBox.IsEnabled" Value="False">
                    <Setter Property="ComboBox.Background" Value="Blue" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Style>
</ComboBox>

Now, I've shown you the IsEnabled Trigger here to demonstrate that you could add multiple Trigger objects like this. However, you can't actually use this Trigger for IsEnabled, because there is a Trigger defined inside the default ComboBox that already has a Trigger set on that property (to make it look disabled). If you absolutely have to add a Trigger for IsEnabled, then you will have to implement your own ControlTemplate for the ComboBox to override that default behaviour. If this is the case, please take a look at the ControlTemplate Class page on MSDN or ask a new question for help with this.

To address your other requirement of your 'view mode' is a bit more tricky. The code that you provided lookslike you are trying to retrieve the value directly from a class, rather than an instance of that class. In WPF, we normally add public properties into a view model or code behind file that we can bind to.

So I would imagine that you could have a bool property named IsViewMode and then you would add another Trigger like this:

                <Trigger Property="IsViewMode" Value="True">
                    <Setter Property="ComboBox.Background" Value="Orange" />
                </Trigger>

However, if your original syntax was correct, then your Trigger would look like this:

                <Trigger Property="wpfMisc:myCtrl.viewMode" Value="True">
                    <Setter Property="ComboBox.Background" Value="Orange" />
                </Trigger>

Upvotes: 1

Related Questions