不同的DataContext指挥和CommandParameter [英] Different Datacontext for Command and CommandParameter
问题描述
是它有可能不同的的DataContext
的WPF 命令
和 CommandParameter
?
<&用户控件GT;
< UserControl.Resources>
<的ViewModels:ListViewGridBaseViewModel X:键=VM/>
< /UserControl.Resources>
<网格和GT;
< ContentControl中X:NAME =currentContent
CONTENT ={绑定路径= ListGrid}>
< ContentControl.ContextMenu>
<&文本菜单GT;
<菜单项命令={绑定路径=保存}
CommandParameter ={绑定的ElementName = currentContent}
的DataContext ={StaticResource的的ResourceKey = VM}
标题=保存>
< MenuItem.Icon>
<图像来源={StaticResource的的ResourceKey =保存}
身高=16
WIDTH =16/>
< /MenuItem.Icon>
< /菜单项>
<菜单项命令={绑定路径=恢复}
的DataContext ={StaticResource的的ResourceKey = VM}
标题=还原>
< MenuItem.Icon>
<图像来源={StaticResource的的ResourceKey =恢复}
身高=16
WIDTH =16/>
< /MenuItem.Icon>
< /菜单项>
< /文本菜单>
< /ContentControl.ContextMenu>
< / ContentControl中>
< /网格和GT;
< /用户控件>
我要的绑定
为ListGrid冒泡到另一个视图模型和命令
到本地视图模型。但 CommandParameter
应该是 ContentControl中
。
LOG是说:
System.Windows.Data错误:4:无法与绑定找到源
引用'的ElementName = currentContent。 BindingEx pression:(无路径);
的DataItem = NULL;目标元素是'菜单项(名称='');
target属性是'CommandParameter'(类型'对象')
文本菜单
突破了的DataContext
继承链,这就是为什么的ElementName = currentContent
无法找到。
在这里寻找人工继承方面,并使用类 DataContextSpy
然后执行以下操作:
<&用户控件GT;
< UserControl.Resources>
<的ViewModels:ListViewGridBaseViewModel X:键=VM/>
<局部:DataContextSpy的DataContext ={绑定的ElementName = currentContent}X:键=间谍>
< /UserControl.Resources>
<网格和GT;
< ContentControl中X:NAME =currentContent
CONTENT ={绑定路径= ListGrid}>
< ContentControl.ContextMenu>
<&文本菜单GT;
<菜单项命令={绑定路径=保存}
CommandParameter ={绑定的DataContext源= {StaticResource的间谍}}
的DataContext ={StaticResource的的ResourceKey = VM}
标题=保存>
< MenuItem.Icon>
<图像来源={StaticResource的的ResourceKey =保存}
身高=16
WIDTH =16/>
< /MenuItem.Icon>
< /菜单项>
<菜单项命令={绑定路径=恢复}
的DataContext ={StaticResource的的ResourceKey = VM}
标题=还原>
< MenuItem.Icon>
<图像来源={StaticResource的的ResourceKey =恢复}
身高=16
WIDTH =16/>
< /MenuItem.Icon>
< /菜单项>
< /文本菜单>
< /ContentControl.ContextMenu>
< / ContentControl中>
< /网格和GT;
< /用户控件>
Is it possible to have a different Datacontext
for a WPF Command
and a CommandParameter
?
<UserControl>
<UserControl.Resources>
<viewmodels:ListViewGridBaseViewModel x:Key="vm" />
</UserControl.Resources>
<Grid>
<ContentControl x:Name="currentContent"
Content="{Binding Path=ListGrid}" >
<ContentControl.ContextMenu>
<ContextMenu >
<MenuItem Command="{Binding Path=Save}"
CommandParameter="{Binding ElementName=currentContent}"
DataContext="{StaticResource ResourceKey=vm}"
Header="Save">
<MenuItem.Icon>
<Image Source="{StaticResource ResourceKey=Save}"
Height="16"
Width="16"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="{Binding Path=Revert}"
DataContext="{StaticResource ResourceKey=vm}"
Header="Revert">
<MenuItem.Icon>
<Image Source="{StaticResource ResourceKey=Revert}"
Height="16"
Width="16"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</ContentControl.ContextMenu>
</ContentControl>
</Grid>
</UserControl>
I want the Binding
for ListGrid bubbled up to another Viewmodel and the Command
to a local ViewModel. But the CommandParameter
should be the ContentControl
.
LOG is saying:
System.Windows.Data Error: 4 : Cannot find source for binding with
reference 'ElementName=currentContent'. BindingExpression:(no path);
DataItem=null; target element is 'MenuItem' (Name='');
target property is 'CommandParameter' (type 'Object')
ContextMenu
breaks the DataContext
inheritance chain, that's why ElementName=currentContent
cannot be found.
Look here for artificial inheritance context and use the class DataContextSpy
then do the following:
<UserControl>
<UserControl.Resources>
<viewmodels:ListViewGridBaseViewModel x:Key="vm" />
<local:DataContextSpy DataContext="{Binding ElementName=currentContent}" x:Key="Spy">
</UserControl.Resources>
<Grid>
<ContentControl x:Name="currentContent"
Content="{Binding Path=ListGrid}" >
<ContentControl.ContextMenu>
<ContextMenu >
<MenuItem Command="{Binding Path=Save}"
CommandParameter="{Binding DataContext,Source={StaticResource Spy}}"
DataContext="{StaticResource ResourceKey=vm}"
Header="Save">
<MenuItem.Icon>
<Image Source="{StaticResource ResourceKey=Save}"
Height="16"
Width="16"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="{Binding Path=Revert}"
DataContext="{StaticResource ResourceKey=vm}"
Header="Revert">
<MenuItem.Icon>
<Image Source="{StaticResource ResourceKey=Revert}"
Height="16"
Width="16"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</ContentControl.ContextMenu>
</ContentControl>
</Grid>
</UserControl>
这篇关于不同的DataContext指挥和CommandParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!