如何使 WPF DataGridCell 只读? [英] How to make WPF DataGridCell ReadOnly?
问题描述
我知道您可以使整个 DataGrid 或一整列就绪 (IsReadOnly = true).但是,在单元级别,此属性仅准备就绪.但我确实需要这种级别的粒度.在过去,DataGrid 是公共领域时,有一篇关于通过更改源代码将 IsReadOnly 添加到一行的博客,但现在我没有 DataGrid 的源代码.有什么解决方法?
I understand you can make the whole DataGrid or a whole column readyonly (IsReadOnly = true). However, at cell level this property is ready only. But I do need this level of granularity. There is blog about adding IsReadOnly to a row by changing the source code in old days when DataGrid was public domain, but now I don't have source code for DataGrid. What's workaround?
禁用单元格 (IsEnabled=false) 几乎可以满足我的需要.但问题是你甚至不能点击禁用的单元格来选择行(我有全行选择模式).
Making cell disabled (IsEnabled=false) almost meets my need. But the problem is that you can't even click the disabled cell to select the row (I have full row selection mode).
由于没有人回答过这个问题,所以我想这不是一个容易解决的问题.这是一个可能的解决方法:使单元格不可编辑.唯一的问题是单击单元格不会选择该行.我刚刚注意到,当单击禁用的单元格时,仍会触发 DataGrid 的 MouseDown 或 MouseUp 事件.在这个事件处理程序中,如果我能找出它点击的行,我可以以编程方式选择该行.但是,我无法弄清楚如何从 DataGrid.InputHitTest
中找到基础行.有人可以给我一些提示吗?
Since nobody has responded to this question, so I guess it's not an easy fix. Here is a possible workaround: Make the cell uneditable. The only problem is that clicking the cell doesn't select the row. I just noticed that MouseDown or MouseUp event of the DataGrid is still fired when the disabled cell is clicked. In this event handler, if I could figure out the row it clicked, I could select the row programmatically. However, I couldn't figure out how to find the underlying row from DataGrid.InputHitTest
. Can somebody please give me some tip?
推荐答案
我遇到了同样的问题,单元格在某些行中应该是只读的,而在其他行中则不是.这是一个变通的解决方案:
I've encountered the same problem, the cell should be read-only in some rows but not in the others. Here is a workaround solution:
想法是在两个模板之间动态切换CellEditingTemplate
,一个和CellTemplate
中的一样,另一个是为了编辑.这使得编辑模式的行为与非编辑单元格完全相同,尽管它处于编辑模式.
The idea is to dynamically switch the CellEditingTemplate
between two templates, one is the same as the one in the CellTemplate
, the other is for editing. This makes the edit mode acts exactly the same as the non-editing cell although it is in edit mode.
以下是执行此操作的一些示例代码,请注意此方法需要 DataGridTemplateColumn
:
The following is some sample code for doing this, notice that this approach requires DataGridTemplateColumn
:
首先,为只读和编辑单元定义两个模板:
First, define two templates for read-only and editing cells:
<DataGrid>
<DataGrid.Resources>
<!-- the non-editing cell -->
<DataTemplate x:Key="ReadonlyCellTemplate">
<TextBlock Text="{Binding MyCellValue}" />
</DataTemplate>
<!-- the editing cell -->
<DataTemplate x:Key="EditableCellTemplate">
<TextBox Text="{Binding MyCellValue}" />
</DataTemplate>
</DataGrid.Resources>
</DataGrid>
然后定义一个带有附加ContentPresenter
层的数据模板,并使用Trigger
切换ContentPresenter
的ContentTemplate
,所以上面两个模板可以通过IsEditable
绑定动态切换:
Then define a data template with additional ContentPresenter
layer and use Trigger
to switch the ContentTemplate
of the ContentPresenter
, so the above two templates can be switched dynamically by the IsEditable
binding:
<DataGridTemplateColumn CellTemplate="{StaticResource ReadonlyCellTemplate}">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<!-- the additional layer of content presenter -->
<ContentPresenter x:Name="Presenter" Content="{Binding}" ContentTemplate="{StaticResource ReadonlyCellTemplate}" />
<DataTemplate.Triggers>
<!-- dynamically switch the content template by IsEditable binding -->
<DataTrigger Binding="{Binding IsEditable}" Value="True">
<Setter TargetName="Presenter" Property="ContentTemplate" Value="{StaticResource EditableCellTemplate}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
HTH
这篇关于如何使 WPF DataGridCell 只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!