带有contextmenu canexecute的WPF列表框在选择之前不会被调用

Dan*_*anW 5 wpf listbox contextmenu commandbinding

我有一个ListBox,ItemTemplate绑定到我的项目的ObservableCollection.目前,我正在尝试实现剪切/复制/粘贴/选择所有(为了简短起见,我只会在这里显示selectall ...)

<UserControl.CommandBindings>
    <CommandBinding Command="SelectAll" CanExecute="SelectAll_CanExecute" Executed="SelectAll_Executed"/>
</UserControl.CommandBindings>
<ListBox x:Name="listbox" 
         ItemsSource="{Binding}" 
         Background="Transparent" 
         SelectionMode="Extended"
         ScrollViewer.VerticalScrollBarVisibility="Auto">
    <ListBox.ContextMenu>
        <ContextMenu>
            <MenuItem Command="SelectAll" />
        </ContextMenu>
    </ListBox.ContextMenu>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Background="Transparent">
                <CheckBox Name="cbEnabled" IsChecked="{Binding Enabled, Mode=TwoWay}" Margin="0,2,0,0"/>
                <TextBlock Text="{Binding Name}" Padding="5,0,0,0"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

这是canexecute的代码隐藏:

    private void SelectAll_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = listbox.Items.Count > 0;
        e.Handled = true;
    }
Run Code Online (Sandbox Code Playgroud)

当我第一次运行程序并右键单击列表框时,始终禁用"全选"上下文菜单(并且从不调用SelectAll_CanExecute),直到我选择了某些内容.有没有什么方法可以让它像它应该的那样工作?(并且无需自动选择第一项或让用户必须这样做)

谢谢!

Vin*_*kal 8

这是这里提到的已知错误.如果在窗口的主要焦点范围没有焦点元素,该CanExecute路由将停在ContextMenu,所以它不会达到的CommandBinding窗口,一个解决办法是绑定MenuItemCommandTarget主窗口,如下面的代码演示:

<ListBox.ContextMenu>
    <ContextMenu>
        <MenuItem Command="SelectAll"
                    CommandTarget="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}">
        </MenuItem>
    </ContextMenu>
</ListBox.ContextMenu>
Run Code Online (Sandbox Code Playgroud)

以下是完整的代码:

<UserControl x:Class="ListBoxStyle.ListBoxUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.CommandBindings>
        <CommandBinding Command="SelectAll" CanExecute="SelectAll_CanExecute" Executed="SelectAll_Executed"/>
    </UserControl.CommandBindings>
    <Grid>
        <ListBox x:Name="listbox" 
             Background="Transparent" 
             SelectionMode="Extended"
             ScrollViewer.VerticalScrollBarVisibility="Auto">
            <ListBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Command="SelectAll"
                    CommandTarget="{Binding Path=PlacementTarget,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}">
                    </MenuItem>
                </ContextMenu>
            </ListBox.ContextMenu>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Background="Transparent">
                        <CheckBox Name="cbEnabled" IsChecked="{Binding Enabled, Mode=TwoWay}" Margin="0,2,0,0"/>
                        <TextBlock Text="{Binding}" Padding="5,0,0,0"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)