假设我有一个带有返回命令的属性的窗口(实际上,它是一个带有ViewModel类中的Command的UserControl,但让我们尽可能简单地重现问题).
以下作品:
<Window x:Class="Window1" ... x:Name="myWindow">
<Menu>
<MenuItem Command="{Binding MyCommand, ElementName=myWindow}" Header="Test" />
</Menu>
</Window>
Run Code Online (Sandbox Code Playgroud)
但以下不起作用.
<Window x:Class="Window1" ... x:Name="myWindow">
<Grid>
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Command="{Binding MyCommand, ElementName=myWindow}" Header="Test" />
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
我得到的错误信息是
System.Windows.Data错误:4:无法找到引用'ElementName = myWindow'的绑定源.BindingExpression:路径= mycommand的; 的DataItem = NULL; target元素是'MenuItem'(Name =''); target属性是'Command'(类型'ICommand')
为什么?我该如何解决这个问题?使用DataContext不是一个选项,因为这个问题发生在可视化树的下方,其中DataContext已经包含正在显示的实际数据.我已经尝试过使用{RelativeSource FindAncestor, ...},但是会产生类似的错误消息.
我正在尝试在ListBox ItemTemplate中获取上下文菜单以调用父视图模型上的方法,并将作为参数单击的项目传入.我有这个工作项目模板中的其他按钮,但对于上下文菜单,它似乎失败了.
我有以下xaml(缩写为清晰):
<ListBox>
<ListBox.GroupStyle>
<GroupStyle>
...
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ContextMenu>
<ContextMenu Name="cm">
<MenuItem Header="Open"
cal:Message.Attach="Open($dataContext)">
</MenuItem>
</Grid.ContextMenu>
<TextBlock VerticalAlignment="Center" >
.. text..
</TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我有一种感觉,这与视觉树不同的事实有关,因此Caliburn无法可靠地解决方法.我确定这是一个常见问题,我尝试了一些我在网上发现的东西,但似乎没有任何效果.
有任何想法吗??
我有一个列表框,我正在为ItemContainer设置样式以包含上下文菜单.这是同样的xaml.
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
...
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
我在ViewModel中编写了目标方法,如下所示.
public void DeleteGroup() { //ToDo
...
}
Run Code Online (Sandbox Code Playgroud)
ViewModel被设置为UserControl的DataContext,其中有ListBox.
上面的代码导致"找不到方法的目标".我不知道为什么这不起作用.我也试过以下变化
<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup"
cal:Action.Target="{Binding ElementName=UCRelayDispositionView, Path=DataContext}">
Run Code Online (Sandbox Code Playgroud)
其中UCRelayDispositionView是UserControl的名称.
为什么上面的代码不起作用?
编辑:1 还尝试了以下内容
<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup"
cal:Action.TargetWithoutContext="{Binding ElementName=UCRelayDispositionView, Path=DataContext}">
Run Code Online (Sandbox Code Playgroud)
还有这个
<MenuItem Header="Remove Group" cal:Message.Attach="DeleteGroup"
cal:Action.TargetWithoutContext="{Binding ElementName=UCRelayDispositionView}">
Run Code Online (Sandbox Code Playgroud)
编辑:2 我试图在ItemContainer上以下列方式使用Tag,但它也不起作用.
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Tag" Value="{Binding Path=DataContext, ElementName=UCRelayDispositionView}"/>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Remove Group"
cal:Message.Attach="DeleteGroup()"
cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}"/>
</ContextMenu>
</Setter.Value> …Run Code Online (Sandbox Code Playgroud)