views:

182

answers:

2

I would like to display a combobox drop down list as a textbox when it's set to be read only. For some reason I can't seem to bind the text of the selected item in the combo box to the textbox. This is my XAML:

 <Style x:Key="EditableDropDown" TargetType="ComboBox">
        <Style.Triggers>
            <Trigger Property="IsReadOnly" Value="True">
                <Setter Property="Background" Value="#FFFFFF" />
                <Setter Property="Template">
                        <Setter.Value>
                        <ControlTemplate TargetType="ComboBox">
                            <TextBox Text="{TemplateBinding SelectedItem, Converter={StaticResource StringCaseConverter}}" 
                                       BorderThickness="0"
                                       Background="Transparent"
                                       FontSize="{TemplateBinding FontSize}" 
                                       HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                       FontFamily="{TemplateBinding FontFamily}"
                                       Width="{TemplateBinding Width}" 
                                       TextWrapping="Wrap"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

<ComboBox IsReadOnly="{Binding ReadOnlyMode}" Style="{StaticResource EditableDropDown}" Margin="0 0 10 0">
       <ComboBoxItem IsSelected="True">Test</ComboBoxItem>
</ComboBox>

When I do this, I get the following as the text:

System.Windows.Controls.ComboBoxItem: Test

I would really appreciate the help!

A: 

You are binding the textboxes .Text property to a ComboBoxItem object. I think your your binding should be something like:

Text="{TemplateBinding SelectedItem.Content, ...}

Or if that doesn't work, make a value converter that extracts whatever text you want to display from the ComboBoxItem object.

Simon P Stevens
Yeah I tried that, and it doesn't work. Is converter the only way to go?
Greg R
@Greg: Well like Jobi has suggested, if you bound your combo box to a collection of objects (ideally strings) then that would work better.
Simon P Stevens
That makes sense, I thought you can get around it with selecteditempath somehow
Greg R
A: 

The result you are getting is right, because you used ComboBoxitem as an item for your ComboBox. Typically in a DataBinding situation you might not need that kind of ComboBoc population. So when you use ComboBox.ItemsSource binding with a CLR collection, you will get the result properly.

Jobi Joy
That worked for me, though I'm still puzzled that I can't do it the other way using the SelectedItemPath
Greg R