takayoshi takayoshi - 9 months ago 112
C# Question

wpf combobox with custom itemtemplate text

I have

ComboBox
with custom
ItemTemplate
.

<ComboBox Height="20" Width="200"
SelectedItem="{Binding Path=SelectedDesign}"
ItemsSource="{Binding Path=Designs}" HorizontalAlignment="Left"
ScrollViewer.CanContentScroll="False">

<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type formdesign:FormDesignContainer}">
<Rectangle Width="200" Height="100">
<Rectangle.Fill>
<ImageBrush ImageSource="{Binding Path=ImageThumb}" Stretch="Uniform" />
</Rectangle.Fill>
</Rectangle>
</DataTemplate>
</ComboBox.ItemTemplate>

</ComboBox>


This works well. However WPF tries to draw rectangle as Combobox Text. How can I set "text" for this template. By "text" I mean string or control which represent selected item and write into combobox when item is selected

In other words I'd like to do this:

enter image description here

But now I got this

enter image description here

Answer Source

Try setting SelectionBoxItemTemplate with a TextBlock. Appears that SelectionBoxItemTemplate is read-only. So another approach is to override ItemContainerStyle.Template. Example