使用MVVM模式单击项目时如何在WPF上下文菜单上获取PlacementTarget [英] How to get the PlacementTarget on WPF Context menu when item click using MVVM pattern
问题描述
使用MVVM模式时如何获取选定的ContextMenu的PlacementTarget?
How to get the PlacementTarget of selected ContextMenu when using MVVM pattern ?
使用MenuItem click事件时,我可以像这样获取PlacementTarget。但是,MVVM是如何做到的呢?
I can get PlacementTarget like this when using MenuItem click event. But how it do it MVVM ?
public void changeGaugeColor(object sender, RoutedEventArgs e)
{
MenuItem tempMenuItem = (MenuItem)sender;
MenuItem mi = (MenuItem)sender;
bd = (BidirectionalDial)cm.PlacementTarget;
}
推荐答案
我意识到认为这是一个过时的帖子,但是有人可能会觉得有用。假设您已将视图绑定到视图模型,则可以通过以下方式将 ContextMenu.DataContext
绑定到视图模型:
I realise that this is an old post, but someone might find this useful to know. Assuming that you have bound your view to your view model, then you can then bind your ContextMenu.DataContext
to your view model in the following way:
首先,将视图命名为 UserControl
...我通常将所有命名为这
为简单起见。然后记住,我们的视图模型绑定到 UserControl
的 DataContext
,我们可以使用 {Binding DataContext,ElementName = This}
。
First, name your view UserControl
... I generally name all of mine This
for simplicity. Then remembering that our view model is bound to the DataContext
of the UserControl
, we can bind to the view model using {Binding DataContext, ElementName=This}
.
现在我们可以绑定到视图模型了,我们必须连接使用 ContextMenu.DataContext
。我将对象的 Tag
属性与 ContextMenu
( PlacementTarget
)作为连接,在此示例中,为 Grid
:
So now we can bind to the view model, we have to connect that with the ContextMenu.DataContext
. I use the Tag
property of the object with the ContextMenu
(the PlacementTarget
) as that connection, in this example, a Grid
:
<DataTemplate x:Key="YourTemplate" DataType="{x:Type DataTypes:YourDataType}">
<Grid ContextMenu="{StaticResource Menu}" Tag="{Binding DataContext,
ElementName=This}">
...
</Grid>
</DataTemplate>
然后我们可以在 ContextMenu中访问视图模型的属性和命令通过将
ContextMenu.DataContext
属性绑定到 PlacementTarget.Tag
属性(网格
):
We can then access the view model properties and commands in the ContextMenu
by binding the ContextMenu.DataContext
property to the PlacementTarget.Tag
property (of the Grid
in our example):
<ContextMenu x:Key="Menu" DataContext="{Binding PlacementTarget.Tag, RelativeSource=
{RelativeSource Self}}">
<MenuItem Header="Delete" Command="{Binding DeleteFile}" CommandParameter=
"{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource
AncestorType=ContextMenu}}" CommandTarget="{Binding PlacementTarget,
RelativeSource={RelativeSource Self}}" />
</ContextMenu>
请注意 MenuItem.CommandTarget
上的绑定属性。设置此项可确保在其上引发指定命令的目标元素是 PlacementTarget
或 Grid
Note the binding on the MenuItem.CommandTarget
property. Setting this ensures that the target element on which the specified command is raised is the PlacementTarget
, or the Grid
in this case.
还要注意 CommandParameter
绑定。这绑定到 PlacementTarget
的 DataContext
或 Grid
在这种情况下。 网格
的 DataContext
将从 DataTemplate
继承这样,您的数据项现在就已绑定到 Command
命令中的 object
参数。 ICommand
接口:
Also note the CommandParameter
binding. This binds to the DataContext
of the PlacementTarget
, or the Grid
in this case. The DataContext
of the Grid
will be inherited from the DataTemplate
and so your data item is now bound to the object
parameter in your Command
if you're using some implementation of the ICommand
interface:
public bool CanExecuteDeleteFileCommand(object parameter)
{
return ((YourDataType)parameter).IsInvalid;
}
public void ExecuteDeleteFileCommand(object parameter)
{
Delete((YourDataType)parameter);
}
或者如果您使用某种 RelayCommand
直接在您的视图模型中进行委托:
Or if you are using some kind of RelayCommand
delegates directly in your view model:
public ICommand Remove
{
get
{
return new ActionCommand(execute => Delete((YourDataType)execute),
canExecute => return ((YourDataType)canExecute).IsInvalid);
}
}
这篇关于使用MVVM模式单击项目时如何在WPF上下文菜单上获取PlacementTarget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!