【发布时间】:2018-10-26 03:49:35
【问题描述】:
我有一个 PdfViewerView.xaml (UserControl),其中包含一个名为“PdfViewerCtrl”的控件。
现在我得到了一个带有上下文菜单的ListBox,如果用户单击上下文菜单项,则一个带有多重绑定触发器的事件:
<ContextMenu>
<MenuItem Header="Löschen"/>
<i:Interaction.Triggers>
<i:EventTrigger
EventName="PreviewMouseDown">
<i:InvokeCommandAction
Command="{Binding DeleteAnnotationCmd}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource MultiBindingConv}">
<Binding ElementName="PdfUserCtrl" Path="PdfViewerCtrl" />
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="PdfViewerCtrl" />
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</ContextMenu>
我正在尝试将PdfViewerCtrl(我在开头提到)作为参数传递,但它始终是DependencyProperty.UnsetValue。
如您所见,我尝试了两种绑定到 PdfViewerCtrl 的方法,但都不起作用。
【问题讨论】:
-
问题是因为
ContextMenu是PopUp。它与它的 onwer 有不同的NameScope,也不是它的 onwer 的可视化树的一部分。所以ElementName和RelativeSource都将无法解决源。 -
您知道如何传递上下文菜单的“所有者”的任何解决方案吗?它是一个列表框
-
ContextMenu.PlacementTarget在打开后应该包含所有者元素。你可以试试<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}" Path="PlacementTarget" />。 -
非常感谢!
标签: wpf xaml mvvm binding multibinding