c# - WPF:将 ListBox ContextMenu 的命令参数绑定(bind)到 ListBox 的选定项

标签 c# wpf data-binding listbox

是否可以将 ListBox 上下文菜单的 CommandParameter 绑定(bind)到 ListBox 的选定项?我应该说 ContCommand 在主窗口中,当单击上下文菜单项时调用它 - 但是,我需要获取参数才能正常工作。

我试过了但是绑定(bind)失败了:

<Window x:Class="ListBoxContextMenu.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ListBoxContextMenu"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <StackPanel>
            <TextBlock Text="ListBox here:"/>
            <ListBox ItemsSource="{Binding Items}" MinHeight="100" TabIndex="0" x:Name="LB">
                <ListBox.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Foo" Command="{Binding ContCommand}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}},Path=SelectedItem}"/>
                    </ContextMenu>
                </ListBox.ContextMenu>
            </ListBox>
        </StackPanel>
    </Grid>
</Window>

主窗口的 C# 代码:

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using MvvmFoundation.Wpf;

    namespace ListBoxContextMenu
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DataContext = this;
                Loaded += (sender, e) => MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                ContCommand = new RelayCommand<object>((object o) =>
                {
                    System.Diagnostics.Debug.WriteLine("Context Menu pressed");
                });
            }

            public ObservableCollection<string> Items { get; set; } = new ObservableCollection<string>{"Fred", "Jim", "Sheila"};
            public RelayCommand<object> ContCommand { get; set; }
        }
    }

最佳答案

ListBox 不是 ContextMenu 的可视祖先,因为后者驻留在自己的可视树中。

但是您可以绑定(bind)到 ContextMenuPlacementTarget,即 ListBox

这个有效:

<ListBox ItemsSource="{Binding Items}" MinHeight="100" TabIndex="0" x:Name="LB">
    <ListBox.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Foo" Command="{Binding ContCommand}" 
                              CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}},
                                Path=PlacementTarget.SelectedItem}"/>
        </ContextMenu>
    </ListBox.ContextMenu>
</ListBox>

关于c# - WPF:将 ListBox ContextMenu 的命令参数绑定(bind)到 ListBox 的选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42564682/

相关文章:

c# - OxyPlot 的 LinearAxis 需要 margin

c# - 在单元格值中绑定(bind) WPF DataGrid 列标题文本

c# - .Net C# : Read attachment from HttpWebResponse

c# - 在隐藏的 Word2010 中创建图表时,Excel 打开。如何禁用它?

c# - 如何在 C# 中读取另一个进程的命令行参数?

c# - CollectionView.Refresh不过滤

c# - 在控制焦点上的 WPF 中打开 AutoCompleteBox

wpf - MEF 和 WPF 自定义导入定义

javascript - 绑定(bind)到 Angular 中的 Controller 对象

c# - 绑定(bind)到 List<string> 不更新相应的字符串