WPF DataGridComboBoxColumn `ComboBox is only available when the DataGrid has IsReadOnly = FALSE

Why is the ComboBox in this column displayed only by double-clicking in an empty cell when the DataGrid is set to IsReadOnly = FALSE ???

<DataGridComboBoxColumn Width="*" IsReadOnly="False" Header="test" /> 

using a DataTemplateColumn works as always ... what's wrong with this DataGridComboBoxColumn?

work:

 <DataGridTemplateColumn Header="Schoolclass"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox Background="Blue" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> 
+4
source share
1 answer

All built-in DataGridColumns have two styles. One of them is when the cell is not in edit mode, and when the cell is in edit mode. Typically, the edit mode does not display the text block, and not the actual control you can expect (ComboBox, TextBox, etc.). And as soon as you start editing the cell, the text block is replaced by the corresponding control. If you have a dataset set equal to IsReadOnly = true, this means that the cells never go into edit mode, and this is the behavior you see.

When creating a DataGridTemplateColumn you need to replace all the built-in datagrid logic. As an example, if you want your template column to be read-only when the datagrid is read-only, you need to manually link the two values ​​together. And if you want to get the same behavior as inline columns (text block when the cell is not in edit mode), you will have to use triggers to provide the appropriate control patterns.

Also note that if you use a built-in column (e.g. DataGridCheckBoxColumn) and use an ElmentStyle for it (e.g. to center the checkbox), then the column cells are edited, even though the dataset is IsReadOnly = true. This is because when you specify an ElmentStyle, you replace the inline style that contains the logic to make read-only flags when the datagrid is read-only.

+10
source

Source: https://habr.com/ru/post/1313195/


All Articles