问题描述
当DataGrid设置为IsReadOnly = FALSE时,为什么只能通过在空白单元格中双击才能看到该列中的ComboBox?
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" />
使用DataTemplateColumn可以像往常一样工作...那个DataGridComboBoxColumn有什么问题?
using a DataTemplateColumn works as always... whats wrong with that DataGridComboBoxColumn?
工作:
<DataGridTemplateColumn Header="Schoolclass">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Background="Blue" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
推荐答案
所有内置的DataGridColumns都有两种样式。一种用于当单元格不处于编辑模式时,另一种用于单元格处于编辑模式时。通常,非编辑模式仅显示文本块,而不显示您可能期望的实际控件(组合框,文本框等)。一旦开始编辑单元格,文本块就会被适当的控件替换。
如果将datagrid设置为IsReadOnly = true,则意味着单元永远不会进入其编辑模式,这就是您所看到的行为。
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.
创建DataGridTemplateColumn时,必须替换所有内置的datagrid逻辑。例如,如果您希望模板列在datagrid为只读时为只读,则必须将两个值手动绑定在一起。
如果想获得与内置列相同的行为(当单元格不在编辑模式时为文本块),则必须使用触发器来提供适当的控件模板。
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.
还请注意,如果您使用内置列(例如DataGridCheckBoxColumn)并且为它专门修饰了ElmentStyle(例如,将复选框居中),那么尽管datagrid设置为IsReadOnly = true。发生这种情况的原因是,当您指定ElmentStyle时,您正在替换内置样式,该样式包含当数据网格为只读时使复选框为只读的逻辑。
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.
这篇关于WPF DataGridComboBoxColumn的ComboBox仅在DataGrid具有IsReadOnly = FALSE时可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!