Datatemplate无法访问到Datatemplate控件层以外的资源.
举例:ContextMenu在技术上是一个单独的窗口,所以它有自己的可视化树并且可能不包含在文档窗格的逻辑树中。因此,它不知道如何从包含视图中找到资源。
你的DataTemplate看起来是在一个ItemsControl使用(如列表框),所以你说Command="{Binding NewProjectCommand}"将试图绑定到Task类型的属性,而你真的想绑定到父容器的属性。因此,您需要使用的RelativeSource约束力的,是这样的:
Command="{Binding Path=DataContext.NewProjectCommand, RelativeSource=
{RelativeSource FindAncestor, AncestorType={x:Type views:ProjectsView}}}"
<ItemsControl x:Name="level1Lister" ItemsSource={Binding MyLevel1List}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content={Binding MyLevel2Property}
Command={Binding ElementName=level1Lister,
Path=DataContext.MyLevel1Command}
CommandParameter={Binding MyLevel2Property}>
</Button>
<DataTemplate>
<ItemsControl.ItemTemplate>
</ItemsControl>
解决DataTemplate无法访问到DataTemplate控件层以外的资源
原文:https://www.cnblogs.com/fishpond816/p/13944683.html