c# - 如何正确地将 CommandParameter 与我的 ContextMenu 绑定到选定的 ListViewItem
问题描述
所以我有一个ListView
,当我右键单击其中的一个项目时,我有一个在单击时触发的命令设置。我希望能够获取所选项目内的文本。所以我想我想将 the绑定SelectedItem
为ListView
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();
}
中继命令
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; }
}
}
解决方案
您可以绑定到 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>
推荐阅读
- python - 如何从我的数据中获取填充的等高线图?
- sql - 如何使用 oracle 表函数将 json 转换为列和行
- python - 在 python 3.7 中使用黑色作为预提交钩子
- apache-kafka - 如何在窗口流式处理中显示中间结果?
- neo4j - Neo4j 合并或合并来自两个全文索引的结果
- arrays - 从元组返回数组
- jpa - MapStruct:如何使用映射器使用复合键 JPA 映射视图对象?
- mule - ESL 强制段忽略组件元素分隔符 ISA16 [mulesoft]
- gtk3 - 如何获取 GLib.Settings 的范围
- reactjs - 在 React JS 中动态获取徽章值