views:

62

answers:

1

Hi, I'm trying to recreate the style of ComboBox in WPF that you see in windows explorer. I'm trying to create something similar to the 'Arrange by:' combobox, which is under Libraries->Documents for example. The combobox has no outline until you hover over it, and when clicked, displays a context menu which allows a single selection.

A: 

You could overwrite the default template. Use Triggers to specific which template to use based on if the combobox has focus or not.

For example:

<Style TargetType="{x:Type ComboBox}">
    <Setter Property="Template" Value="{StaticResource TemplateWhenFocused}" />
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsMouseOver" Value="False" />
                <Condition Property="IsFocused" Value="False" />
            </MultiTrigger.Conditions>
            <Setter Property="Template" Value="{StaticResource TemplateWhenNotFocused}" />
        </MultiTrigger>
    </Style.Triggers>
</Style>

Example Non-Focused Template. May need to adjust margin to avoid the text "jumping" around when switching focus on or off the object.

<ControlTemplate TargetType="{x:Type ComboBox}" x:Key="StyleWhenNotFocused">
    <TextBlock Text="{TemplateBinding Text}"
               Foreground="{TemplateBinding Foreground}"
               Background="{TemplateBinding Background}"
               Padding="{TemplateBinding Padding}"
               Margin="2,0,2,0" />
</ControlTemplate>
Rachel