Skip to content

DataGridTemplateColumn should have independent edit events #4223

@michael-hawker

Description

@michael-hawker

Came from discussion here: #4206 (review)

Describe the bug

DataGridTemplateColumn is hard to customize as it expects to take DataTemplates nicely from XAML, but then has overrides for a subclass on its methods like PrepareCellForEdit and CancelCellEdit vs. having event based properties. To customize this behavior you have to hook into PreparingCellForEdit event on the DataGrid and then look for the specific column you want vs. having registered the event on the column directly for the one case you may want to override.

Expected behavior

Able to modify the behavior of a base DataGridTemplateColumn without sub-classing.

Additional context

I propose we add two new events to the DataGridTemplateColumn class (or maybe even the base DataGridColumn: PrepareCellForEdit and CancelCellEdit which could be called after the subclass has been called in the Internal methods here:

internal void CancelCellEditInternal(FrameworkElement editingElement, object uneditedValue)
{
CancelCellEdit(editingElement, uneditedValue);
}

internal object PrepareCellForEditInternal(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
{
return PrepareCellForEdit(editingElement, editingEventArgs);
}

The sender would be the column and we could construct event args which contain the returned object from the subclass, the editing element, and the other editing args?

This would make it easier to customize a specific column (and/or also have a general override behavior for every column) without having to continually check for specific columns in the main DataGrid PreparingCellForEdit event?

Currently what we have to do:

dataGrid.PreparingCellForEdit += DataGrid_PreparingCellForEdit;

private void DataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
    if (e.Column is DataGridTemplateColumn column && (string)column?.Tag == "First_ascent" &&
        e.EditingElement is CalendarDatePicker calendar)
    {
        calendar.IsCalendarOpen = true;
    }
}

Proposal (add event to Column directly from XAML):

private void DataGridTemplateColumn_PreparingCellForEdit(DataGridTemplateColumn sender, DataGridColumnPreparingCellForEditEventArgs e)
{
    if (e.EditingElement is CalendarDatePicker calendar)
    {
        calendar.IsCalendarOpen = true;
    }
}

Thoughts, concerns @RBrid?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DataGrid 🔠Issues on DataGrid controlfeature request 📬A request for new changes to improve functionality

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions