Home > database >  Hiding wpf ContextMenu when the Data Grid is Empty?
Hiding wpf ContextMenu when the Data Grid is Empty?

Time:01-05

I am trying to make a simple TODO App with WPF using MVVM. I have the tasks in Datagrid and a right click context menu with Delete option. I don't want the context menu to show when there are no elements in Data Grid. What can I do to add a condition to it?

   <DataGrid  ItemsSource="{Binding Path=TaskList}" SelectedItem="{Binding SelectedTask}" AutoGenerateColumns="False">

            <DataGrid.ContextMenu  >
                <ContextMenu>
                    <MenuItem Header="Delete" Command="{Binding OpenDialogCommand}"  CommandParameter="{Binding PlacementTarget.SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"></MenuItem>
                </ContextMenu>
            </DataGrid.ContextMenu>

            <DataGrid.Columns>
                <DataGridTextColumn Header="Priority" Width="auto" Binding="{Binding Path=Id}"/>
                
                <DataGridTextColumn Header="Task" Width="auto" Binding="{Binding Path=TaskName}"/>              
            </DataGrid.Columns>

           
        </DataGrid>

CodePudding user response:

You could set the ContextMenu property conditionally using a Style with a Trigger:

<DataGrid ItemsSource="{Binding Path=TaskList}" SelectedItem="{Binding SelectedTask}" AutoGenerateColumns="False">
    <DataGrid.Style>
        <Style TargetType="DataGrid">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <MenuItem Header="Delete" Command="{Binding OpenDialogCommand}"  
                                      CommandParameter="{Binding PlacementTarget.SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"></MenuItem>
                    </ContextMenu>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="HasItems" Value="False">
                    <Setter Property="ContextMenu" Value="{x:Null}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Style>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Priority" Width="auto" Binding="{Binding Path=Id}"/>
        <DataGridTextColumn Header="Task" Width="auto" Binding="{Binding Path=TaskName}"/>
    </DataGrid.Columns>
</DataGrid>

CodePudding user response:

You could bind visibility and use the built in booleantovisibility converter

You need the converter. This could be in a resource dictionary you merge in app.xaml to use across the app.

  <Window.Resources>
    <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter"/>
  </Window.Resources>

Then just bind visibility on the context menu

<DataGrid ...... >
    <DataGrid.ContextMenu>
        <ContextMenu Visibility="{Binding PlacementTarget.HasItems, RelativeSource={RelativeSource Self}, 
                                  Converter={StaticResource booleanToVisibilityConverter}}">
             <MenuItem Header="Delete" 
                       Command="{Binding OpenDialogCommand}"  
                       CommandParameter="{Binding PlacementTarget.SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
        </ContextMenu>
    </DataGrid.ContextMenu>
  • Related