How to display combo box as textbox in WPF via a style template trigger?
I would like to display a combobox drop down list as a textbox when 开发者_StackOverflow中文版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!
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.
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.
精彩评论