开发者

Conditionally making readonly to WPF DataGridCell

I have 开发者_Go百科a situation that needs to conditionally make readonly to wpf datagrid cell. There is IsReadOnly property in DataGridCell. But unfortunately, that property is readonly! Is there any way to do it?

ant.


You should be able to use the DataGrid.BeginningEdit event to conditionally check if the cell is editable and then set the Cancel property on the event args if not.


The similar solution as Goblin above, but with a little code samples:

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.

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>

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


Another very simple solution to this problem is to use a Style of the 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>

This style assumes that there is a IsEnabled property in the ViewModel.

This does not make the cell read only but disabled. It is almost the same thing except that is cannot be selected. This solution might not be applicable in all cases due to this.


You could also use the TemplateSelector property to set two different DataTemplates (one writable and one readonly) based on your logic? Just create a class that inherits from DataTemplateSelector and override the SelectTemplate() method (here you have access to the datacontext).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜