问题描述
我正在使用Caliburn Micro作为MVVM框架的WPF项目,在过去的一周中我很幸运地找到了StackOverflow上任何问题的所有解决方案,但是现在我面临着一个更大的问题
I'm working on a WPF project using Caliburn Micro as a framework for MVVM, and during the last week I've been lucky enough to find every solution to any problem on StackOverflow, but now I'm facing a bigger issue that I can't solve by myself.
我有一个包含TreeView的View;在以下情况下,树视图的每个项目都应调用一个方法:
I have a View containing a TreeView; each item of the treeview should invoke a method when:
- 双击[工作]
- 单击上下文菜单项[不起作用]
这是TreeView:
This is the TreeView:
<TreeView x:Name="projectTreeView"
Visibility="{Binding ExplorerVisibility, Converter={StaticResource visibilityConverter}}">
<TreeViewItem Header="{Binding ProjectName}" IsExpanded="True">
<TreeViewItem Header="Category 1"/>
<TreeViewItem Header="Category 2" ItemsSource="{Binding Images}">
<TreeViewItem.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Remove"
cal:Action.TargetWithoutContext="{Binding Path=DataContext, ElementName=projectTreeView}"
cal:Message.Attach="[Event Click] = [Action RemoveResource()]"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</TreeViewItem.ItemContainerStyle>
<TreeViewItem.ItemTemplate>
<HierarchicalDataTemplate>
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
<Style.Triggers>
<EventTrigger RoutedEvent="Collapsed">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames
Duration="0"
Storyboard.TargetProperty="(TreeViewItem.IsExpanded)">
<DiscreteBooleanKeyFrame KeyTime="0" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
<ContentControl cal:Action.TargetWithoutContext="{Binding Path=DataContext, ElementName=projectTreeView}"
cal:Message.Attach="[Event MouseDoubleClick] = [Action OpenResource(projectTreeView.SelectedItem)]">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ResourceName}" Margin="5,0,0,0"/>
</StackPanel>
</ContentControl>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
</TreeViewItem>
</TreeView>
附加的ViewModel包含两种方法:
The attached ViewModel contains both methods:
public class MyViewModel
{
...
public void OpenResource(object obj) { ... }
public void RemoveResource() { ... }
}
由于某种原因 OpenResource 可以完美运行,而当我单击上下文菜单项(右键单击后)时,应用程序崩溃,但出现以下异常:
For some reason OpenResource works perfectly, while when I click on the context menu item (after right click) the application crashes with the exception:
An unhandled exception of type 'System.Exception' occurred in WindowsBase.dll
Additional information: No target found for method RemoveResource.
我在论坛和支持论坛上发现了与同一问题有关的东西,
I found something related to the same problem here on the forum and on the support forum, but I was not able to fix the problem with those tips.
您对我的TreeView中发生的事情有任何了解吗?
Do you have any idea about what's going on in my TreeView?
非常感谢您的帮助!
推荐答案
ContextMenu
驻留在自己的可视树中,并且无法使用 ElementName
绑定到 TreeView
。
The ContextMenu
resides in its own visual tree and can't bind to the TreeView
using an ElementName
.
您可以尝试将 TreeViewItem
的 Tag
属性绑定到父级 TreeView
,然后使用 ContextMenu $ c $的
PlacementTarget
属性绑定到它c>:
You could try to bind the Tag
property of the TreeViewItem
to the parent TreeView
and then bind to it using the PlacementTarget
property of the ContextMenu
:
<Style TargetType="TreeViewItem">
<Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=TreeView}}" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Remove"
cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag.DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"
cal:Message.Attach="[Event Click] = [Action RemoveResource()]"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
这篇关于ItemContainerStyle内的Caliburn Micro Action-未找到方法的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!