如何将CommandParameter和ContextMenu正确绑定到选定的ListViewItem



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

我注意到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();
}

RelayCommand

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; }
        }
    }

您可以绑定到ContextMenuPlacementTarget(StackPanel(,然后获得其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>

最新更新