rPulvi
rPulvi

Reputation: 946

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.

Upvotes: 0

Views: 2531

Answers (2)

Rohit Vats
Rohit Vats

Reputation: 81253

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

Upvotes: 4

Mario Vernari
Mario Vernari

Reputation: 7304

Try to modify the setter as follows:

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

Upvotes: 0

Related Questions