Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridComboBoxColumn cell not displaying selected item text?

I was wondering how you get the DataGridComboBoxColumn to display the selected item text when it is not in editing mode? And also is it possible to make it so the combo box in edit mode displays the selected item text initially?

Here is my XAML:

<DataGridComboBoxColumn Header="Formatter" 
                        SelectedItemBinding="{Binding Path=Format}">
  <DataGridComboBoxColumn.ElementStyle>
    <Style TargetType="">
      <Setter Property="Text" Value="{Binding Path=FormatView.Name}" />
    </Style>
  </DataGridComboBoxColumn.ElementStyle>
  <DataGridComboBoxColumn.EditingElementStyle>
    <Style TargetType="ComboBox">
      <Setter Property="ItemsSource" Value="{Binding Path=DefinedFormatters}" />
      <Setter Property="IsDropDownOpen" Value="True" />
      <Setter Property="ItemTemplate">
        <Setter.Value>
          <DataTemplate>
            <TextBlock Text="{Binding Path=Name}"></TextBlock>
          </DataTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
like image 820
Alex Hope O'Connor Avatar asked Jan 20 '23 08:01

Alex Hope O'Connor


2 Answers

This was the only resource on DataGridComboBoxColumn that I found helpful:

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcomboboxcolumn.aspx

Everything else (that I found on non-MSDN sites) was misleading!

like image 72
Gerry Avatar answered Jan 21 '23 22:01

Gerry


Ok I figured it out after a lot of googling.

but it seems you just do the same thing for ElementStyle, again with the target type of combo box, even though it does not seem to show a ComboBox when not editing.

<DataGridComboBoxColumn Header="Formatter" SelectedItemBinding="{Binding Path=Format}">
        <DataGridComboBoxColumn.ElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding Path=DefinedFormatters}" />
                <Setter Property="IsDropDownOpen" Value="True" />
                <Setter Property="ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}"></TextBlock>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGridComboBoxColumn.ElementStyle>
        <DataGridComboBoxColumn.EditingElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding Path=DefinedFormatters}" />
                <Setter Property="IsDropDownOpen" Value="True" />
                <Setter Property="ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}"></TextBlock>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGridComboBoxColumn.EditingElementStyle>
    </DataGridComboBoxColumn>
like image 44
Alex Hope O'Connor Avatar answered Jan 21 '23 23:01

Alex Hope O'Connor