我有一种情况需要有条件地只读wpf datagrid单元格。 DataGridCell中有IsReadOnly属性。但不幸的是,该属性是只读的!有什么办法可以做到吗?
蚂蚁。有条件地只读WPF DataGridCell
回答
您应该可以使用DataGrid.BeginningEdit事件来有条件地检查单元格是否可编辑,如果没有,则在事件参数上设置Cancel属性。
您还可以使用TemplateSelector属性根据您的逻辑设置两个不同的DataTemplates(一个是可写的,一个是只读的)?只需创建一个继承自DataTemplateSelector的类并覆盖SelectTemplate()方法(此处您可以访问datacontext)。
的类似的解决方案如上述地精,但有一点代码样本:
的想法是动态地切换模板CellEditingTemplate
两者之间,一个是相同的所述一个在CellTemplate
,另一种是用于编辑。这使得编辑模式与非编辑单元完全相同,尽管它处于编辑模式。
以下是这样做的一些示例代码,请注意,这种方法需要DataGridTemplateColumn
:
首先,定义两个模板只读和编辑细胞:
<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
绑定动态切换:
<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
+1为WPF唯一解决方案:) – 2013-04-19 04:39:52
另一种非常简单的解决这个问题是使用的DataGridCell
<DataGrid>
<DataGrid.Resources>
<Style x:Key="disabledCellStyle" TargetType="DataGridCell">
<Setter Property="IsEnabled" Value="{Binding IsEnabled}" />
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn CellStyle="{StaticResource disabledCellStyle}" />
<DataGridCheckBoxColumn CellStyle="{StaticResource disabledCellStyle}" />
<DataGridTextColumn/> /*always enabled*/
</DataGrid.Columns>
</DataGrid>
这种风格样式假设有在一个视图模型IsEnabled属性。
这不会使单元格只读但禁用。除了无法选择外,它几乎是一样的。由于此原因,此解决方案可能不适用于所有情况。
- 1. wpf toggle datagridcell只读
- 2. WPF DataGridCell中的条件格式
- 3. WPF有DatagridRow和DataGridCell
- 4. WPF DatagridCell事件触发器
- 5. WPF DataGridCell保证金
- 6. 带有TextBlock的WPF DataGridCell模板 - 绑定?
- 7. WPF DataGridCell LostFocus事件停止冒泡DataGridRow
- 8. 有条件地设置可编辑或只读文本字段
- 9. 如何有条件地使DetailsView字段为只读?
- 10. GridView模板列有条件地设置为只读
- 11. 绑定WPF DatagridCell到源
- 12. WPF datagridcell的datacontext不会留
- 13. WPF DataGrid绑定DataGridCell内容
- 14. Excel条件只读字段
- 15. DataGridTextboxcolumn只读基于条件
- 16. 只用CSS有条件地隐藏div?
- 17. 从Firebase有条件地读取数据
- 18. 以wpf制作WebBrowser控件只读?
- 19. 将文本包裹在DataGridCell中WPF
- 20. WPF工具包DataGridCell样式DataTrigger
- 21. 如何访问DataGridCell上点击WPF DataGrid
- 22. WPF - MouseDown通过DataGridCell上的AutomationPeer
- 23. 动态绑定WPF DataGridCell模板
- 24. 有条件的XAML(WPF)
- 25. WPF有条件绑定
- 26. WPF ListBox有条件DisplayMemberPath
- 27. 如何有条件地指定HTML输入是禁用/只读还是不是?
- 28. 如何使WPF WebBrowser只读?
- 29. WPF ListView与“只读”SelectedItems
- 30. 读用fread只有一条线从data.table
IsReadOnly是一个布尔结果,用于检查单元格是否只读或只读,这就是为什么它是只读属性:) – VoodooChild 2010-06-01 03:33:15
IsReadOnly属性在作为datagrid的根元素的DataGrid上读取/写入。 IsReadOnly属性不能在DataGrid的子元素中设置,例如DataGridCell,因为DataGrid.IsReadyOnly为false时将DataGridCell.IsReadOnly设置为true没有意义 – VoodooChild 2010-06-01 03:42:43
@VoodooChild无法在DataGridCell上设置IsReadOnly的主要原因是因为它们是暂时的。它们是根据需要创建和丢弃的,因为DataGrid在任何时间点都不会将其所有行和单元保存在内存中。所以即使设置了这个属性,即使它是可写的也没有地方可以设置。 – Josh 2010-06-01 03:59:54