stefan
stefan

Reputation: 1376

wpf DataGrid disable cell edit for specific row except first column

I have a binding to a generic DataTable where some rows may contain problems and should not be modified. There seems to be a very easy way to accomplish that by simple setting IsEnabled in DataGrid_LoadingRow or by a Trigger with Binding in XAML. However, I also have a “Info”-Column which has a Button in it so that the user can view the problem… Setting IsEnabled to false also disables my button… I already tried: IsManipulationEnabled but this has no effect. How can I accomplish this? Is the only way for doing that by traversing the VisualTree of the DataRowView and set IsEnabled for all Cells except the first?

Upvotes: 0

Views: 786

Answers (1)

AnjumSKhan
AnjumSKhan

Reputation: 9827

For your first column, use this Button,

public class EnabledButton : Button
{
    static EnabledButton()
    {
        UIElement.IsEnabledProperty.OverrideMetadata(typeof(EnabledButton),
                new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.None,
                    UIElement.IsEnabledProperty.DefaultMetadata.PropertyChangedCallback,
                    new CoerceValueCallback(IsEnabledCoerceCallback)));
    }

    private static object IsEnabledCoerceCallback(DependencyObject d, object baseValue)
    {
        return (bool)baseValue;
    }
}

Usage :

<DataGrid.Columns>
    <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <local:EnabledButton IsEnabled="True" Content="Modify"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    ...
</DataGrid.Columns>

This overrides the inheritance of DataGridCell.IsEnabled property.

Upvotes: 1

Related Questions