简体   繁体   English

WPF XAML-将上下文菜单项绑定到主窗口DataContext

[英]WPF XAML - Bind Context Menu Item to Main Window DataContext

I have a Window with the DataContext binded to a ViewModel. 我有一个DataContext绑定到ViewModel的窗口。 In my ViewModel I have a Command named for example 在我的ViewModel中,我有一个命令,例如

HideShowSingleWindow

My Window has a context menu for the tray icont that is dynamically filled. 我的窗口有一个用于动态填充任务栏图标的上下文菜单。 Now I need to bind the Command on MenuItem click to the HideShowSingleWindow command in the Window datacontext. 现在,我需要将MenuItem单击上的Command绑定到Window数据上下文中的HideShowSingleWindow命令。

I tried 我试过了

<Grid>                        
     <tb:TaskbarIcon
      IconSource="/Icons/main.ico"
      ToolTipText="SCADA Control Center" 
            DoubleClickCommand="{Binding Path=HideShow}">
            <tb:TaskbarIcon.ContextMenu>
                <ContextMenu>
                    <ContextMenu.ItemsSource>
                        <CompositeCollection>
                            <MenuItem Header="Windows" ItemsSource="{Binding Path=RegisteredWindows}">
                                <MenuItem.ItemContainerStyle>
                                    <Style TargetType="{x:Type MenuItem}">
                                        <Setter Property="Header" Value="{Binding Path=Title}" />
                                        <Setter Property="IsCheckable" Value="True" />
                                        <Setter Property="IsChecked" Value="{Binding Path=IsLoaded, Mode=OneWay}"/>
                                        <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=HideShowSingleWindow}" />
                                        <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}" />
                                    </Style>
                                </MenuItem.ItemContainerStyle>
                            </MenuItem>
                            <MenuItem Header="Show/Hide All" Command="{Binding Path=HideShow}" />
                            <Separator />
                            <MenuItem Header="Exit" Command="{Binding Path=Quit}" />
                        </CompositeCollection>
                    </ContextMenu.ItemsSource>
                </ContextMenu>
            </tb:TaskbarIcon.ContextMenu>
     </tb:TaskbarIcon>
</Grid>

Where we can see: 我们在哪里可以看到:

<Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=HideShowSingleWindow}" />

but it doesn't work. 但这不起作用。

ContextMenu doesn't inherit DataContext of tb:TaskbarIcon because context menu doesn't lie in same Visual tree as that of its placement target ( taskbar icon in your case ). ContextMenu不继承tb:TaskbarIcon DataContext,因为上下文菜单与其放置目标在您的情况下为任务栏图标不在同一个Visual树中

So, get the DataContext explicitly and bind with command like this: 因此,显式获取DataContext并使用以下命令进行绑定:

<Setter Property="Command"
        Value="{Binding RelativeSource={RelativeSource FindAncestor,
                         AncestorType={x:Type ContextMenu}}, 
                       Path=PlacementTarget.DataContext.HideShowSingleWindow}"/>

尝试按以下方式修改设置器:

<Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type tb:TaskbarIcon}}, Path=DataContext.HideShowSingleWindow}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM