问题 有条不紊地为只读WPF DataGridCell


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


12933
2018-06-01 03:27


起源

IsReadOnly是一个布尔结果,用于检查单元格是否为readOnly或not,这就是为什么它是readonly属性:) - VoodooChild
IsReadOnly属性是在DataGrid上读/写的,DataGrid是datagrid的根元素。 IsReadOnly属性不能在DataGrid的子元素中设置,例如DataGridCell,因为当DataGrid.IsReadyOnly为false时将DataGridCell.IsReadOnly设置为true没有意义 - VoodooChild
@VoodooChild你不能在DataGridCell上设置IsReadOnly的主要原因是它们是瞬态的。它们是根据需要创建和丢弃的,因为DataGrid不会在任何时间点将所有行和单元格保留在内存中。因此,即使它是可写的,也没有地方可以设置此属性。 - Josh
stackoverflow.com/questions/2030143/... - Mohsen


答案:


你应该可以使用 DataGrid.BeginningEdit 有条件地检查单元格是否可编辑的事件,然后在事件args上设置Cancel属性,如果没有。


7
2018-06-01 03:37





与上面的Goblin类似的解决方案,但有一些代码示例:

想法是动态切换 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 切换 ContentTemplate 的 ContentPresenter,所以上面两个模板可以动态切换 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


7
2017-08-19 02:28



+1仅WPF解决方案:) - Black Stallion


解决此问题的另一个非常简单的方法是使用DataGridCell的Style

<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>

此样式假定ViewModel中存在IsEnabled属性。

这不会使单元格只读,但禁用。除了无法选择之外几乎是一回事。由于这个原因,此解决方案可能不适用于所有情况。


2
2018-04-01 12:08



这不适用于单个单元格 - 仅适用于整列... - Sven
@Sven它对我有用...... - MgSam
不适用于单个细胞。 - Maxime Tremblay-Savard


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


0
2018-06-01 10:27