简体   繁体   中英

WPF XAML - Bind Context Menu Item to Main Window DataContext

I have a Window with the DataContext binded to a ViewModel. In my ViewModel I have a Command named for example

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.

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 ).

So, get the DataContext explicitly and bind with command like this:

<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}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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