WPF notifyicon context menu using applicationcommands is disabled

630 views Asked by At

I have a WPF PRISM application with a mainwindow based on a ribbinwindow. When I right click the icon in the icontray the exit menu is disabled. Can someone show me how to make it enabled and handle the close event appropriately.

I am showing the whole code of the mainwindow but it is the context menu of the notify icon that is giving me a headache. I found the placementTarget commandtarget binding by trying to google a fix for this problem, but this does not seem to work.
The ribbonbutton that is also binded the same way as the context menu does work as expected.

I'd prefer to keep the command binding and not handle an event in this situation because I am using MVVM. Closing could be handled by simply calling an event and calling close in that event without having a separation of concern issue, but I am going to use other commands as well. So no events please.

<ribbon:RibbonWindow x:Class="_2Focus.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ribbon="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon" 
    xmlns:prism="http://www.codeplex.com/prism"
    xmlns:tb="http://www.hardcodet.net/taskbar"
    Title="2Focus" Icon="/2Focus;component/favicon.ico"
    x:Name="Mainwindow"
    Height="500" Width="900">
<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition x:Name="RibbonRow" Height="Auto"/>
        <RowDefinition x:Name="ClientRow" Height="*"/>
    </Grid.RowDefinitions>
    <tb:TaskbarIcon IconSource="/2Focus;component/favicon.ico" ToolTipText="2 Focus" >
        <tb:TaskbarIcon.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Exit" Command="ApplicationCommands.Close" 
                          CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}">
                    <MenuItem.Icon>
                        <Image Source="Images/exit.png" SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Second Menu Item" />
            </ContextMenu>
        </tb:TaskbarIcon.ContextMenu>
    </tb:TaskbarIcon>
    <ribbon:Ribbon x:Name="Ribbon" prism:RegionManager.RegionName="RibbonRegion" SelectionChanged="Ribbon_SelectionChanged" >
        <ribbon:Ribbon.QuickAccessToolBar>
            <ribbon:RibbonQuickAccessToolBar Name="ribbonQuickAccessToolBar" AllowDrop="True" />
        </ribbon:Ribbon.QuickAccessToolBar>
        <ribbon:Ribbon.ApplicationMenu>
            <ribbon:RibbonApplicationMenu SmallImageSource="/2Focus;component/favicon.ico">
                <ribbon:RibbonApplicationMenu.FooterPaneContent>
                    <DockPanel LastChildFill="False">
                        <ribbon:RibbonButton DockPanel.Dock="Right" Margin="2" Command="ApplicationCommands.Close" SmallImageSource="Images/exit.png" />
                    </DockPanel>
                </ribbon:RibbonApplicationMenu.FooterPaneContent>
            </ribbon:RibbonApplicationMenu>
        </ribbon:Ribbon.ApplicationMenu>
    </ribbon:Ribbon>
    <Grid x:Name="ClientGrid" Grid.Row="1">
        <ContentControl prism:RegionManager.RegionName="ClientRegion" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    </Grid>
</Grid>

0

There are 0 answers