2

I'd like to have a combobox in a datagrid to show a list of actual images, instead of text.

I can make this work by manually building a combobox, but cant seem to do this via binding (which is about the only way the datagrid can be used).

I also tried a template column, but got the same results- listing of text showing the name of the image class. Any samples demonstrating this?

Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87
Brady Moritz
  • 8,624
  • 8
  • 66
  • 100

1 Answers1

5

Nest as many templates as you need, if your ComboBox shows the class name just set ComboBox.ItemTemplate to do something with your class. Here i assume that MyImageList consists of ImageSource objects (needs some more sizing specifications):

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding MyImageList}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Image Source="{Binding}"/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Alternatively you could porbably use a DataGridComboBoxColumn as well, just use the CellStyle to set up a DataTemplate which can display your images:

<DataGridComboBoxColumn ItemsSource="{Binding MyImageList}">
    <DataGridComboBoxColumn.CellStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Image Source="{Binding}"/>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridComboBoxColumn.CellStyle>
</DataGridComboBoxColumn>
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Thanks HB. I ran out of time and just did a manual buildup, but will impl this later as time permits. – Brady Moritz Feb 21 '11 at 16:27
  • This allowed me to programmatically generate DataGridComboBoxColum's using same strategy as discussed in this question's answer: https://stackoverflow.com/questions/59379995/show-image-in-dynamically-generated-datagrid-columns-in-wpf/59390358#59390358. The former worked best for me. – Will Marcouiller Dec 19 '19 at 12:14