WPF DataGridComboBoxColumn 的 ComboBox 仅在 DataGrid 具有 IsReadOnly=FALSE 时可见

发布于 2024-09-06 12:32:59 字数 646 浏览 2 评论 0原文

当 DataGrid 设置为 IsReadOnly = FALSE 时,为什么该列中的 ComboBox 仅通过双击空单元格可见?

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

使用 DataTemplateColumn 一如既往地工作... DataGridComboBoxColumn 有什么问题吗?

作品:

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2024-09-13 12:32:59

所有内置 DataGridColumns 都有两种样式。一种用于单元格未处于编辑模式时,另一种用于单元格处于编辑模式时。通常,非编辑模式仅显示文本块,而不是您可能期望的实际控件(ComboBox、TextBox 等)。一旦开始编辑单元格,文本块就会被替换为适当的控件。
如果您将数据网格设置为 IsReadOnly = true,则意味着单元格永远不会进入其编辑模式,这就是您所看到的行为。

创建 DataGridTemplateColumn 时,您本质上是替换所有内置的数据网格逻辑。例如,如果您希望当数据网格为只读时模板化列为只读,则必须手动将两个值绑定在一起。
如果您想获得与内置列相同的行为(当单元格未处于编辑模式时为文本块),那么您必须使用触发器来提供适当的控制模板。

另请注意,如果您使用内置列(例如 DataGridCheckBoxColumn)并且为其指定 ElmentStyle(例如将复选框居中),则尽管 datagrid 设置为 IsReadOnly = true,但该列的单元格都是可编辑的。发生这种情况是因为当您指定 ElmentStyle 时,您将替换内置 Style,其中包含当数据网格只读时使复选框只读的逻辑。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文