Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WPF - bind MenuItem Command to ICommand property? #68

Open
erikthysell opened this issue Jan 31, 2023 · 1 comment
Open

WPF - bind MenuItem Command to ICommand property? #68

erikthysell opened this issue Jan 31, 2023 · 1 comment

Comments

@erikthysell
Copy link

What would you like to be added:

I am probably doing something wrong but, using TaskbarIcon from H.NotifyIcon.Wpf, I would like to ha a MenuItem Command binding to a IRelayCommand (from CommunityToolkit.Mvvm.Input) property of the encompassing window, i.e.:

MainWindow.xaml :

<Window x:Class="MyProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyProject"
        xmlns:tb="clr-namespace:H.NotifyIcon;assembly=H.NotifyIcon.Wpf" 
        mc:Ignorable="d"
        Title="My Project" Height="450" Width="800"
        ShowInTaskbar="False">

    <Grid>
        <tb:TaskbarIcon
        ToolTipText="DANSIM Cluster Client"
            IconSource="/Images/dscc.ico"
        MenuActivation="LeftOrRightClick"
        PopupActivation="DoubleClick">
            <tb:TaskbarIcon.ContextMenu>
                <ContextMenu>
                    <TextBlock Text="Special commands" />
                   
                    <MenuItem
                        Header="_Quit"
                        Command="{Binding QuitCommand, RelativeSource={RelativeSource AncestorType=Window}}">
                    </MenuItem>
                </ContextMenu>
            </tb:TaskbarIcon.ContextMenu>
        </tb:TaskbarIcon>
    </Grid>
</Window>

MainWindow.xaml.cs:

using CommunityToolkit.Mvvm.Input;
using Serilog;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Forms;

namespace MyProject
{
public partial class MainWindow : Window
    {        public IRelayCommand QuitCommand { get; set; }

        public MainWindow()
        {
            Log.Logger = new LoggerConfiguration()
						//.WriteTo.RichTextBox(LogTextBox)
						.WriteTo.Console()
						.WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
						.CreateLogger();
            Log.Information("Starting MyProject");
            ShowLogWindowCommand = new RelayCommand(ShowLogWindow);
            QuitCommand = new RelayCommand(Quit);

            InitializeComponent();
            }
        }

        private void OnClicked(object? sender, MouseEventArgs e)
        {
            App.Current.MainWindow.Show();
        }

        private void Quit(object? sender, EventArgs e)
        {
            Log.Information("Closing app");
            this.Close();
        }
    }
}

Why is this needed:

To aid in mvvm scenarios

For which Platform:

  • [x ] WPF

Anything else we need to know?

@HavenDV
Copy link
Owner

HavenDV commented Feb 2, 2023

You need to specify the DataContext for Window({Binding RelativeSource={RelativeSource Self}}) or TaskbarIcon({Binding RelativeSource={RelativeSource AncestorType=Window}}).
The reason the current code doesn't work is because the ContextMenu doesn't belong to the window's visual tree as far as I know. Therefore, it does not have access to Window. But it will inherit DataContext from TaskbarIcon, because this case is taken into account in the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants