首页 > 解决方案 > 如何正确地将 CommandParameter 与我的 ContextMenu 绑定到选定的 ListViewItem

问题描述

所以我有一个ListView,当我右键单击其中的一个项目时,我有一个在单击时触发的命令设置。我希望能够获取所选项目内的文本。所以我想我想将 the绑定SelectedItemListViewCommandParameter

我注意到它ContextMenu不在视觉树中,这使事情变得更加混乱。

它目前正在返回null

<ListView x:Name="PlayerListView"
                  Width="200"
                  Height="330"
                  VerticalAlignment="Top"
                  Margin="0,80,15,0"
                  HorizontalAlignment="Right"
                  Background="#252525"
                  VerticalContentAlignment="Center"
                  ItemsSource="{Binding ServerViewModel.Players}">


            <ListView.ItemTemplate>
                <DataTemplate DataType="{x:Type model:PlayerModel}">
                    <StackPanel Orientation="Horizontal" 
                                VerticalAlignment="Stretch" 
                                HorizontalAlignment="Stretch"
                                Width="190"
                                Background="#222222">

                        <StackPanel.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Command One"
                                          DataContext="{Binding DataContext, 
                                          Source={mvvm:RootObject}}" 
                                          Command="{Binding ServerViewModel.MyCommand}"
                                          CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu},
                                            Path=PlacementTarget.SelectedItem}">
                                    <MenuItem.Icon>
                                        <Image Source="../../Assets/image.png"
                                               RenderOptions.BitmapScalingMode="Fant"
/>
                                    </MenuItem.Icon>
                                </MenuItem>
                            </ContextMenu>
                        </StackPanel.ContextMenu>

                        <Image Source="../../Assets/image.png"
                               Width="20"
                               Height="20"/>

                        <TextBlock Text="{Binding Username}" 
                                   Foreground="White"
                                   HorizontalAlignment="Stretch"
                                   VerticalAlignment="Center" 
                                   Margin="5"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

该命令工作正常,一切正常,但它返回 null,我知道这是因为 XAML 是如何设置的,但我不确定如何遍历 DOM

DataContext 是这样设置的

public MainWindow()
{
    InitializeComponent();
    DataContext = new BaseViewModel();
}

中继命令

public class RelayCommand : ObservableObject, ICommand
    {
        private readonly Action<object> _execute;
        private readonly Predicate<object> _canExecute;

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        public RelayCommand(Action<object> execute) : this(execute, null)
        {

        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            //This is where I debug and see that it returns null
            _execute.Invoke(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }

标签: c#wpfmvvmdata-bindingcommand

解决方案


您可以绑定到 the PlacementTarget( StackPanel) ,ContextMenu然后获取它的DataContext

<DataTemplate DataType="{x:Type model:PlayerModel}">
    <StackPanel Orientation="Horizontal" 
                VerticalAlignment="Stretch" 
                HorizontalAlignment="Stretch"
                Width="190"
                Background="#222222"
                Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ListView}}">
        <StackPanel.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Command One"
                        Command="{Binding PlacementTarget.Tag.ServerViewModel.MyCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                        CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                    <MenuItem.Icon>
                        <Image Source="../../Assets/image.png" RenderOptions.BitmapScalingMode="Fant"/>
                    </MenuItem.Icon>
                </MenuItem>
            </ContextMenu>
        </StackPanel.ContextMenu>

        <Image Source="../../Assets/image.png" Width="20" Height="20"/>

        <TextBlock Text="{Binding Username}" 
                Foreground="White"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Center" 
                Margin="5"/>
    </StackPanel>
</DataTemplate>

推荐阅读