我正在尝试在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无法可靠地解决方法.我确定这是一个常见问题,我尝试了一些我在网上发现的东西,但似乎没有任何效果.
有任何想法吗??
我用对象填充了ListView,并将ContextMenu绑定到ListView中的那些项目.只能通过单击项目来打开ContextMenu.问题是Caliburn Micro抛出一个错误,它无法找到ShowProperties()的目标方法.
我认为出现此问题是因为Caliburn没有可用的ViewModel的正确DataContext.我在Stackoverflow上尝试了很多解决方案,使ViewModel可用于ContextMenu项目,但无济于事,例如:
Caliburn Message.Attach()抛出"找不到方法的目标"
这是我的观点的XAML代码:
<Window x:Class="CueMaster.Views.AppView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dragDrop="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
xmlns:cal="http://www.caliburnproject.org"
Height="500" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListView Grid.Column="1" Margin="5"
ItemsSource="{Binding Cues}"
dragDrop:DragDrop.IsDragSource="True"
dragDrop:DragDrop.IsDropTarget="True"
dragDrop:DragDrop.DropHandler="{Binding}">
<ListView.Resources>
<ContextMenu x:Key="ItemContextMenu">
<MenuItem Header="Properties" cal:Message.Attach="ShowProperties($dataContext)" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}">
<MenuItem.Icon>
<Image Source="../PropertyIcon.png" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</ListView.Resources>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}" >
<Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView >
<GridViewColumn Width="70" Header="Cue" DisplayMemberBinding="{Binding Id}" />
<GridViewColumn Width="100" …Run Code Online (Sandbox Code Playgroud)