wpf 用于删除列表视图中项目的上下文菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11082162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
context menu for removing items in listview
提问by M.E.
I have a ListView which displays a list of string values. I want to add a context menu entry for each item in the list to remove the selected item. My XAML looks like this:
我有一个显示字符串值列表的 ListView。我想为列表中的每个项目添加一个上下文菜单条目以删除所选项目。我的 XAML 看起来像这样:
<ListView x:Name="itemsListView" ItemsSource="{Binding MyItems}">
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove"
Command="{Binding RemoveItem}"
CommandParameter="{Binding ElementName=itemsListView, Path=SelectedItem}" />
</ContextMenu>
</ListView.ContextMenu>
</ListView>
The problem is that the CommandParameter
value is always null. I've added an additional button to remove the selected item to check if my command works. The button has exactly the same binding and removing items via the button works. The button looks like this:
问题是该CommandParameter
值始终为空。我添加了一个额外的按钮来删除所选项目以检查我的命令是否有效。该按钮具有完全相同的绑定和通过按钮工作删除项目。按钮看起来像这样:
<Button Content="Remove selected item"
Command="{Binding RemoveItem}"
CommandParameter="{Binding ElementName=itemsListView, Path=SelectedItem}"/>
The command looks like this:
该命令如下所示:
private ICommand _removeItem;
public ICommand RemoveItem
{
get { return _removeItem ?? (_removeItem = new RelayCommand(p => RemoveItemCommand((string)p))); }
}
private void RemoveItemCommand(string item)
{
if(!string.IsNullOrEmpty(item))
MyItems.Remove(item);
}
Any ideas why the selected item is null when opening the context menu? Maybe a focus problem of the listview?
打开上下文菜单时为什么所选项目为空的任何想法?也许是列表视图的焦点问题?
回答by blindmeis
H.B. is right. but you can also use RelativeSource Binding
HB是对的。但你也可以使用RelativeSource Binding
<ListView x:Name="itemsListView" ItemsSource="{Binding MyItems}">
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove"
Command="{Binding RemoveItem}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" />
</ContextMenu>
</ListView.ContextMenu>
</ListView>
回答by H.B.
ContextMenus
are disconnected, you cannot use ElementName
bindings. One workaround would be using Binding.Source
and x:Reference
which requires you to extract parts that use it to be in the resources (due to cyclical dependency errors). You can just put the whole context menu there.
ContextMenus
断开连接,您不能使用ElementName
绑定。一种解决方法是使用Binding.Source
并且x:Reference
需要您提取使用它的部分以将其放入资源中(由于循环依赖错误)。您可以将整个上下文菜单放在那里。
An example:
一个例子:
<ListBox Name="lb" Height="200">
<ListBox.Resources>
<ContextMenu x:Key="cm">
<MenuItem Header="{Binding ActualHeight, Source={x:Reference lb}}" />
</ContextMenu>
</ListBox.Resources>
<ListBox.ContextMenu>
<StaticResource ResourceKey="cm" />
</ListBox.ContextMenu>
</ListBox>
回答by Vivek Malik
This work for me CommandParameter="{Binding}"
这对我有用 CommandParameter="{Binding}"