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

时间:2018-11-13 01:48:02

标签: c# wpf mvvm data-binding command

所以我有一个ListView,当我右键单击其中的项目时,我有一个命令设置,单击该命令会触发。 我希望能够获取所选项目内的文本。 所以我想我想将SelectedItem的{​​{1}}绑定为ListView

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

当前正在返回ContextMenu

null

该命令运行正常,所有命令均正常运行,但返回null,我知道这是因为XAML的设置方式,但是我不确定如何遍历DOM

DataContext这样设置

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

The RelayCommand

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

1 个答案:

答案 0 :(得分:1)

您可以绑定到StackPanel的{​​{1}}(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>