Search code examples
c#wpfdatagriddatagridcomboboxcolumn

DataGridComboBoxColumn opens after 2 mouse clicks


I have a datagrid and it has DataGridComboBoxColumn like below. When i open datagrid, 2 mouse clicks show the combox. How to show combobox when datagrid is loaded without click.

<DataGrid  x:Name="customLoadCaseGrid" 
              ItemsSource="{Binding Source={StaticResource weightItemCollection}}"
              CanUserSortColumns="False"  SelectionMode="Single"  AutoGenerateColumns="False"  Margin="5,5,5,5"
              CanUserDeleteRows="False"
              Grid.Row="1" Grid.Column="0" Grid.RowSpan="19" Grid.ColumnSpan="20"  
              CellEditEnding="DataGridCellEditEnding" PreviewKeyDown="DatagridPreviewKeyDown" 
              BeginningEdit="DatagridBeginningEdit" BorderThickness="2" >

<DataGrid.Columns>

 <DataGridComboBoxColumn Header="{lex:LocText Key=fsm_type, Dict=language, Assembly=StabilityGui}" SortMemberPath="FsmTypes" Width="1*" Visibility="Visible"> 
                <DataGridComboBoxColumn.ElementStyle>
                    <Style>
                        <Setter Property="ComboBox.ItemsSource" Value="{Binding Path=FsmTypes}" />
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style>
                        <Setter Property="ComboBox.ItemsSource" Value="{Binding Path=FsmTypes}" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>

Solution

  • If you want to display a ComboBox in the CellTemplate, you should replace the DataGridComboBoxColumn with a DataGridTemplateColumn:

    <DataGrid ...>
        <DataGrid.Resources>
            <DataTemplate x:Key="template">
                <ComboBox ItemsSource="{Binding Path=FsmTypes, RelativeSource={RelativeSource AncestorType=Window}}" />
            </DataTemplate>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTemplateColumn ... CellTemplate="{StaticResource template}" CellEditingTemplate="{StaticResource template}" />
            ...
        </DataGrid.Columns>
    </DataGrid>
    

    You can open one of the ComboBoxes by setting the IsDropDownOpen property to true. You cannot have more than one ComboBox open at the same time though.