2010-06-01 36 views
11

我有一种情况需要有条件地只读wpf datagrid单元格。 DataGridCell中有IsReadOnly属性。但不幸的是,该属性是只读的!有什么办法可以做到吗?
蚂蚁。有条件地只读WPF DataGridCell

+0

IsReadOnly是一个布尔结果,用于检查单元格是否只读或只读,这就是为什么它是只读属性:) – VoodooChild 2010-06-01 03:33:15

+0

IsReadOnly属性在作为datagrid的根元素的DataGrid上读取/写入。 IsReadOnly属性不能在DataGrid的子元素中设置,例如DataGridCell,因为DataGrid.IsReadyOnly为false时将DataGridCell.IsReadOnly设置为true没有意义 – VoodooChild 2010-06-01 03:42:43

+1

@VoodooChild无法在DataGridCell上设置IsReadOnly的主要原因是因为它们是暂时的。它们是根据需要创建和丢弃的,因为DataGrid在任何时间点都不会将其所有行和单元保存在内存中。所以即使设置了这个属性,即使它是可写的也没有地方可以设置。 – Josh 2010-06-01 03:59:54

回答

7

您应该可以使用DataGrid.BeginningEdit事件来有条件地检查单元格是否可编辑,如果没有,则在事件参数上设置Cancel属性。

0

您还可以使用TemplateSelector属性根据您的逻辑设置两个不同的DataTemplates(一个是可写的,一个是只读的)?只需创建一个继承自DataTemplateSelector的类并覆盖SelectTemplate()方法(此处您可以访问datacontext)。

7

的类似的解决方案如上述地精,但有一点代码样本:

的想法是动态地切换模板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来切换ContentPresenterContentTemplate,所以上述两个模板可以通过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

+0

+1为WPF唯一解决方案:) – 2013-04-19 04:39:52

2

另一种非常简单的解决这个问题是使用的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

这不适用于单个单元格 - 仅适用于整列。 .. – Sven 2014-11-19 14:21:10

+0

@Sven它为我工作... – MgSam 2015-03-13 17:17:20

+1

不适用于单个细胞。 – 2015-07-13 19:59:32