4

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

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
takayoshi
  • 2,789
  • 5
  • 36
  • 56
  • Can you please post the code of the type of the items you put into the ComboBox? I think I don't fully understand the problem yet. – Spontifixus Dec 04 '12 at 09:19
  • My object derived from Canvas – takayoshi Dec 04 '12 at 09:46
  • So the `Designs`-property is of some kind of `IEnumerable`? Where does the text you want to display come from? – Spontifixus Dec 04 '12 at 09:49
  • Name property. As I know Canvas also have Name – takayoshi Dec 04 '12 at 09:50
  • I want to display rectangles in drop-down list, and as text I'd like to show Name property – takayoshi Dec 04 '12 at 09:52
  • @Spontifixus look at update – takayoshi Dec 04 '12 at 10:02
  • 1
    Now your question is clear. I don't have time for an elaborate answer at the moment, but you will need to create a control template for the entire `ComboBox` showing the string "Design" when it is focused. Keep in mind that this is not a default behavior of a windows combo box - and thus not what a user might expect. – Spontifixus Dec 04 '12 at 10:10

3 Answers3

1

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

Community
  • 1
  • 1
aadam
  • 713
  • 5
  • 18
0

I found this solution by Ray Burns a good approach. You can define two DataTemplate one for Items in the drop down list and the other for the selected item which should be shown in the Combobox. The using a trigger and checking the visual tree it decide which one to use.

<Window.Resources>    
  <DataTemplate x:Key="NormalItemTemplate" ...>
    ...
  </DataTemplate>

  <DataTemplate x:Key="SelectionBoxTemplate" ...>
    ...
  </DataTemplate>

  <DataTemplate x:Key="CombinedTemplate">
    <ContentPresenter x:Name="Presenter"
       Content="{Binding}"
       ContentTemplate="{StaticResource NormalItemTemplate}" />
    <DataTemplate.Triggers>
      <DataTrigger
        Binding="{Binding RelativeSource={RelativeSource FindAncestor,ComboBoxItem,1}}"
        Value="{x:Null}">
        <Setter TargetName="Presenter" Property="ContentTemplate"
                Value="{StaticResource SelectionBoxTemplate}" />
      </DataTrigger>
    </DataTemplate.Triggers>
  </DataTemplate>

</Window.Resources>

...

<ComboBox
  ItemTemplate="{StaticResource CombinedTemplate}"
  ItemsSource="..."/>
Community
  • 1
  • 1
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
-1

Add Textblock to the datatemplate and bind it or add Contentpersenter on the rectangle Edit: it seems like i didn't got what you were tring to accomplish ,

ZSH
  • 905
  • 5
  • 15