1

I'm customizing a WPF DataGridCell style, but once I've done this double click to edit no longer works. I've also tried to manually call myDataGrid.BeginEdit, but nothing seems to happen. This is code similar to what I have:

<Style x:Key="MyCell" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
    <Setter Property="Template">
         <Setter.Value>
             <ControlTemplate>
                 <Border>
                     <TextBlock />
                 </Border>
             </ControlTemplate>
         </Setter.Value>
    </Setter>
</Style>

I assume the problem is that because I'm overriding the ControlTemplate, an editor no longer exists? If that's the case, is their a way I can sort this to use the default editor?

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
Kyle
  • 17,317
  • 32
  • 140
  • 246
  • If you aren't overriding the cell edit template then it should work fine, also make sure `CanUserEditRows` is set to `True` on your Datagrid. I have to see your Datagrid's XAML to further understand your problem. – Abdul Rafay May 14 '20 at 19:12
  • Why are you changing the `ControlTemplate` in the first place? What are you trying to achieve? Usually you can do everything you need in a `DataGrid` using `DataTemplate`s. – Keith Stein May 15 '20 at 01:16
  • @KeithStein I'm using a subclassed TextBlock which sets different foreground colours for individual words in the content string. I couldn't figure out how to do that without replacing the `ControlTemplate`. – Kyle May 15 '20 at 10:32

2 Answers2

2

I'm using a subclassed TextBlock which sets different foreground colours for individual words in the content string. I couldn't figure out how to do that without replacing the ControlTemplate.

Instead of creating a custom ControlTemplate , you should create a custom column type and override the GenerateElement method to return an instance of your custom type:

public class CustomColumn : DataGridTextColumn
{
    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        return new YourCustomTextBlock();
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
1

Generally, you should avoid overriding ControlTemplates unless you really need to make substantial changes to the functionality or layout of a control (or if your building your own). You can usually do what you need with DataTemplates instead, which keep the underlying control intact, but customize the way your data is displayed.

For the DataGrid, there is a special type of column type meant just for this: DataGridTemplateColumn. Here's a quick example of how to use it:

<DataGrid>
    <DataGrid.Columns>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <!--This is used when the cell is being displayed normally-->
                <DataTemplate>
                    <TextBlock Text="{Binding SomePropertyOfYourRow}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <!--This is used when the cell is being edited (e.g. after double-click)-->
                <DataTemplate>
                    <TextBox Text="{Binding SomePropertyOfYourRow}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

You can easily put your custom TextBlock into the CellTemplate and it will be use to display the contents of that cell.

Keith Stein
  • 6,235
  • 4
  • 17
  • 36