WPF DataGridComboBoxColumn`s ComboBox is only visible when DataGrid has IsReadOnly=FALSE

最后都变了- 提交于 2019-12-06 04:45:57

问题


Why is the ComboBox in that column only visible via double-click in the empty cell when the DataGrid is set to IsReadOnly = FALSE ???

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

using a DataTemplateColumn works as always... whats wrong with that DataGridComboBoxColumn?

works:

<DataGridTemplateColumn Header="Schoolclass">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox Background="Blue" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

回答1:


All builtin DataGridColumns have two styles. One for when the cell is not in editing mode and one where the cell is in editing mode. Usually the non editing mode simply displays a textblock, not the actual control you might expect (ComboBox, TextBox, etc). And once you start editing the cell, the textblock is replaced with the appropriate control. If you have the datagrid set to IsReadOnly = true, then that means the cells never go to their editing mode and that is the behaviour you are seeing.

When creating a DataGridTemplateColumn you are essentialy replacing all the built in datagrid logic. As an example if you want your templated column to be readonly when the datagrid is readonly, then you have to manually bind the two values together. And if you wanted to get the same behaviour as the builtin columns (textblock when cell is not in editing mode), then you'd have to use triggers to supply the appropriate controltemplates.

Also note, that if you are using a built in column (eg DataGridCheckBoxColumn) and you speficy an ElmentStyle for it (for instance to center the checkBoxes) then the column's cells are all editable despite datagrid being set to IsReadOnly = true. This happens because when you specify an ElmentStyle you are replacing the builtin Style, which contains logic to make the checkboxes readonly when the datagrid is readonly.



来源:https://stackoverflow.com/questions/3070455/wpf-datagridcomboboxcolumns-combobox-is-only-visible-when-datagrid-has-isreadon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!