Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Datagrid Row Context Menu - Disable Menu Item [closed]

Hi There I am beginner programmer and new to WPF and have a simple question, but I have spent quite some time searching it a could not figure it out, so I hope you guys will help me. All I want is to disable menu item in a context menu in my Datagrid. for example: if more than one rows selected in Datagrid , disable a context menu item "Properties"

<DataGrid.Resources>
    <ContextMenu  x:Key="DataRowContextMenu">
            <MenuItem x:Name="RowContMenuProp"  Header="Properties">
                <MenuItem.Icon>
                    <Image Source="Resources/proporties.ico" Height="16" Width="16" />
                </MenuItem.Icon>
            </MenuItem>
            <Separator Margin="0"  />
            <MenuItem Header="Copy" Command="Copy" >
                <MenuItem.Icon>
                    <Image Source="Resources/copy.ico" Height="16" Width="16" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Remove from list" Click="MenuItem_Click_1" >
                <MenuItem.Icon>
                    <Image Source="Resources/cut.png" Height="16" Width="16" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Remove from project" Click="MenuItem_Click_2" >
                <MenuItem.Icon>
                    <Image Source="Resources/remove.ico" Height="16" Width="16" />
                </MenuItem.Icon>
            </MenuItem>
    </ContextMenu>
</DataGrid.Resources>

<DataGrid.RowStyle >
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="ContextMenu" Value="{StaticResource DataRowContextMenu}" />
        <Setter Property="BorderThickness" Value="0"/>
    </Style>
</DataGrid.RowStyle>

-- disable Context menu item

Private Sub datagrid1_MouseUp(sender As Object, e As MouseButtonEventArgs)
        If datagrid1.SelectedItems.Count > 1 Then
like image 463
Dude Avatar asked Sep 30 '22 23:09

Dude


1 Answers

This is one way to go about it. It is a bit dirty but works.

First create an IValueConverter to say that 1 means enable (true) and the Value Converter may look like this

public class OneReturnsTrueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (int)value == 1;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

Then save a reference to the DataGrid in your DataGridRow

<DataGrid.RowStyle >
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
        <Setter Property="ContextMenu" Value="{StaticResource DataRowContextMenu}" />
        <Setter Property="BorderThickness" Value="0"/>
    </Style>
</DataGrid.RowStyle>

And finally bind the SelectedItems count of the grid to the IsEnabled property

<DataGrid.Resources>
    <myConverters:OneReturnsTrueConverter x:Key="OneReturnsTrueConverter"/>
    <ContextMenu  x:Key="DataRowContextMenu">
        <MenuItem x:Name="RowContMenuProp"  Header="Properties"
                  DataContext="{Binding Parent.PlacementTarget.Tag , RelativeSource={RelativeSource Self}}"
                  IsEnabled="{Binding Path=SelectedItems.Count, Converter={StaticResource OneReturnsTrueConverter}}" />
    </ContextMenu>
</DataGrid.Resources>
like image 69
dharshana jagoda Avatar answered Oct 03 '22 02:10

dharshana jagoda