tags:

views:

117

answers:

1

Pretty much I have an Editable Combobox and I want to add a button to the right of the drop down button which clears the selected item. So...

|TextBox |X|v|

I was thinking something like...

<Style...>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <ControlTemplate.Resources>
                    <Style TargetType="{x:Type TextBox}">...Add button here...</Style>
                </ControlTemplate.Resources>
                <ContentPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
+2  A: 

I do not think that your proposed approach will work. As soon as you set the Template property in the style, you will also have to redefine its visual representation. You could, however, define an adjusted ControlTemplate for a TextBox (as you suggested) in the Resources section of the ComboBox (not within its Template!).

<ComboBox ...>
    <ComboBox.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Template">
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <!-- define template for TextBox with an additional "clear" button -->
                </ControlTemplate>
            </Setter>
        </Style>
    </ComboBox.Resources>
</ComboBox>

But then you still have the problem how to handle the click on this button...

A clean solution would be to sub-class ComboBox and provide an ICommand which clears the current selection. Then you would override the ComboBox's ControlTemplate, add a "clear" button besides the drop down button and bind that button to the new ICommand. This definitely is quite some work, but in the end you have a clean solution which you can easily extend later on.

gehho
Good answer. Re-templating for this sort of experience is pretty standard and the way to do it... and through extra visuals in the template with code-behind event bindings, commands, or new sub-classes, they all get you there.
Jeff Wilcox