Louitbol
Louitbol

Reputation: 170

WPF Disabled DatagridRow and ContextMenu

I have a datagrid binded to a collection of custom objects.

This datagrid allows the user to access a context menu when he right clicks a row. I do this through TextBlock styling:

<Style x:Key="DatagridTextblockStyle"
    TargetType="{x:Type TextBlock}">
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Header="First action" />
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>

It also displays rows which might be disabled if the custom object's "IsActive" bool property is false.

I do this through the DataGrid.RowStyle:

<DataGrid ItemsSource="{Binding MyCustomObjects}">
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsActive}"
                             Value="True">
                    <Setter Property="IsEnabled"
                            Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

This works fine. The problem however is that when a row is disabled, the context menu is not available anymore.

I can't find a way around that.

Any ideas?

Upvotes: 1

Views: 241

Answers (1)

mm8
mm8

Reputation: 169320

Set the ContextMenuService.ShowOnDisabled attached property to true in the ElementStyle:

<Style x:Key="DatagridTextblockStyle" TargetType="{x:Type TextBlock}">
    <Setter Property="ContextMenuService.ShowOnDisabled" Value="True" />
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Header="First action" />
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>

Upvotes: 1

Related Questions