【问题标题】:Multibinding unset value多重绑定未设置值
【发布时间】: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 的方法,但都不起作用。

【问题讨论】:

  • 问题是因为ContextMenuPopUp。它与它的 onwer 有不同的NameScope,也不是它的 onwer 的可视化树的一部分。所以ElementNameRelativeSource 都将无法解决源。
  • 您知道如何传递上下文菜单的“所有者”的任何解决方案吗?它是一个列表框
  • ContextMenu.PlacementTarget 在打开后应该包含所有者元素。你可以试试&lt;Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}" Path="PlacementTarget" /&gt;
  • 非常感谢!

标签: wpf xaml mvvm binding multibinding


【解决方案1】:

尝试使用x:Reference 标记扩展将Tag 属性设置为PdfViewerCtrl,然后绑定到ContextMenuTag 属性:

<ContextMenu Tag="{x:Reference PdfViewerCtrl}">
    <MenuItem Header="Löschen"/>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="PreviewMouseDown">
            <i:InvokeCommandAction Command="{Binding DeleteAnnotationCmd}">
                <i:InvokeCommandAction.CommandParameter>
                    <MultiBinding Converter="{StaticResource MultiBindingConv}">
                        <Binding RelativeSource="{RelativeSource AncestorType=ContextMenu}" Path="Tag" />
                    </MultiBinding>
                </i:InvokeCommandAction.CommandParameter>
            </i:InvokeCommandAction>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ContextMenu>

【讨论】:

  • 非常感谢!!
  • 还有一个问题,也许你也可以帮助我。我还尝试传递列表框(ContextMenu 的“父级”)。最聪明的方法是什么?
  • 如果您还有其他问题,请提出新问题。如果可以的话,我很乐意为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 2019-12-17
  • 2014-01-25
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
  • 2013-10-30
  • 2014-12-30
  • 1970-01-01
相关资源
最近更新 更多