数据网格为空时隐藏wpf上下文菜单?

jpfvwuh4  于 2023-01-06  发布在  其他
关注(0)|答案(3)|浏览(214)

我正在尝试使用MVVM制作一个简单的带有WPF的TODO应用程序。我在Datagrid中有任务,并有一个带有删除选项的右键单击上下文菜单。我不希望在数据网格中没有元素时显示上下文菜单。我该怎么做才能向它添加条件?

<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>
bpsygsoo

bpsygsoo1#

可以使用StyleTrigger有条件地设置ContextMenu属性:

<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>
lndjwyie

lndjwyie2#

您可以绑定可见性并使用内置的布尔型自动可见性转换器
你需要转换器,它可能在一个资源字典中,你可以合并到app.xaml中,以便在整个应用程序中使用。

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

然后只需在上下文菜单上绑定可见性

<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>
goucqfw6

goucqfw63#

作为变体之一,可以使用ContextMenuService.IsEnabled attached属性并将其与ItemsControl.HasItems绑定。

<DataGrid ...
          ContextMenuService.IsEnabled="{Binding RelativeSource={RelativeSource Self}, Path=HasItems}">

相关问题