1

I have a UserControl with Button inside which opens a ContextMenu when left clicked. I'm trying to pass UserControl's parent Window as a parameter to ContextMenu item's command to close that window, but with no avail. I've tried everything with RelativeSource and PlacementTarget, but parameter is always null. I'm aware that ContextMenu is not part of parent window's VisualTree. I'm currently stuck with this approach, but it is not working.

<Grid x:Name="LayoutRoot">
        <Button 
            HorizontalAlignment="Left" 
            Margin="0" 
            Style="{DynamicResource ButtonStyle1}" 
            VerticalAlignment="Top" 
            Width="120" 
            Height="25" 
            Content="Dashboard Menu" 
            TextElement.FontWeight="Bold" 
            Foreground="AliceBlue"             
            >

            <!--Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={ x:Type Window}}}"-->
            <Button.ContextMenu>
                <ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" >
                    <MenuItem Header="Open Log Viewer" Command="{StaticResource openLogViewer}" />
                    <Separator />
                    <MenuItem Header="Exit" Command="{StaticResource exit}" CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=Window}}"/>
                </ContextMenu>
            </Button.ContextMenu>
        </Button>
    </Grid>

Command is a Referenced command defined in UserControl.Resources:

<my:CommandReference x:Key="exit" Command="{Binding Exit}" />

and it's Execute part is triggered but parameter is always null. So, my question is, what is the right way to bind parent window as CommandParameter of MenuItem. Any help is appreciated, because this thing is bothering me for almost two days.

4

1 回答 1

2

正确的方法是不要将父Window级作为CommandParameter. 如果这是 MVVM,您应该使用 Messenger(MVVM Light) / EventAggregator(Prism) 方法在Window触发命令以关闭它时将消息发送到 's 的代码隐藏。

在虚拟机中引用Window是完全错误的。

仅供参考,您尝试做的事情“可以做到”

就像是:

<Grid x:Name="LayoutRoot">
  <Button HorizontalAlignment="Left" 
          Margin="0" 
          Style="{DynamicResource ButtonStyle1}" 
          VerticalAlignment="Top" 
          Width="120" 
          Height="25" 
          Content="Dashboard Menu" 
          TextElement.FontWeight="Bold" 
          Foreground="AliceBlue"
          Tag="{Binding RelativeSource={RelativeSource FindAncestor,
                                                         AncestorType={x:Type Window}}}">
    <Button.ContextMenu>
      <ContextMenu>
        <MenuItem Header="Open Log Viewer" Command="{StaticResource openLogViewer}" />
        <Separator />
        <MenuItem Command="{StaticResource exit}"
                  CommandParameter="{Binding PlacementTarget.Tag,
                                             RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"
                  Header="Exit" />
...

更新:

下载链接

从 执行“退出”命令时,ContextMenu您应该Sender Object: MvvmLight16.MainWindow在输出窗口中看到。此输出是从 VM 发送的。

于 2013-06-19T11:59:33.420 回答