WPF NotifyIcon

September 22nd, 2009

This is an implementation of a NotifyIcon (aka system tray icon or taskbar icon) for the WPF platform. It does not just rely on the Windows Forms NotifyIcon component, but is a purely independent control which leverages several features of the WPF framework in order to display rich ToolTips, Popups, context menus, and balloon messages. It can be used directly in code or embedded in any XAML file.

image

 

Download Control & Sample Application

(Latest release: 1.0.4, 2009.09.22)

 

Features at a glance

  • Custom Popups (interactive controls) on mouse clicks.
  • Customized ToolTips (Vista and above) with fallback mechanism for xp/2003.
  • Rich event model including attached events to trigger animations in Popups, ToolTips, and balloon messages. I just love that.
  • Full support for standard Windows balloons, including custom icons.
  • Custom balloons that pop up in the tray area. Go wild with styles and animations :)
  • Support for WPF context menus.
  • You can define whether to show Popups on left-, right-, double-clicks etc. The same goes for context menus.
  • Simple data binding for Popups, ToolTips and custom balloons through attached properties and derived data context.
  • Command support for single / double clicks on the tray icon.

    Screenshots

    The screenshots below were taken from NetDrives and the sample application.

    quickaccess

     

    image

     

    image

     

    image

     

    image

     

    XAML Declaration Sample

    The sample below shows some of the properties of the control. For a more comprehensive sample, have a look at the sample application that comes with the download.

    <Window
      x:Class="Hardcodet.NetDrives.UI.SystemTray.Sample"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:tb="http://www.hardcodet.net/taskbar">
    
        <tb:TaskbarIcon x:Name="myNotifyIcon"
                        Visibility="Visible"
                        ToolTipText="Fallback ToolTip for Windows xp"
                        IconSource="/Images/TrayIcons/Logo.ico"
                        ContextMenu="{StaticResource TrayMenu}"
                        MenuActivation="LeftOrRightClick"
                        TrayPopup="{StaticResoure TrayStatusPopup}"
                        PopupActivation="DoubleClick"
                        TrayToolTip="{StaticResource TrayToolTip}"
          />
    
    </Window>
    1. James
      February 3rd, 2011 at 09:08 | #1

      Philipp - Nice work. Quick question. Is there an easy way to just use this as a nice tooltip replacement to get balloon tips on any WPF object? (Paths in my case and I need to do in in the code behind)

      I am creating ToolTips programmatically and then calling ToolTipService.SetShowOnDisabled to assign them to my Paths. It would be nice to do something similar with your Balloon popup! Any way to do so?

    2. February 4th, 2011 at 04:23 | #2

      James,

      The balloons are also just standard popups - you can display them for any control by just setting the PlacementTarget - just check their usage in the control, which is pretty trivial.

      Cheers,
      Philipp

    3. Mark
      February 25th, 2011 at 06:19 | #3

      Hi,
      Great control, thanks for your efforts.
      I made a small modification. I found the positioning of context menu and custom popup a little odd. I am surprised nobody else has seen this. Maybe its because I use two screens. However, changing the placement to PlacementMode.MousePoint mad it work much better:

      /*
      //use absolute position
      ContextMenu.Placement = PlacementMode.AbsolutePoint;
      ContextMenu.HorizontalOffset = cursorPosition.X;
      ContextMenu.VerticalOffset = cursorPosition.Y;
      */
      // FIX: use mousepoint as the positioning - works better
      ContextMenu.Placement = PlacementMode.MousePoint;

    4. anonymous
      March 6th, 2011 at 22:38 | #4

      Vista built-in system tray icons like Power, Date/Time, Volume, Network had rich tooltips with icons but Windows 7 has plain tooltips for system tray icons. Can you bring rich tooltips for system icons on Windows 7?

    5. Keith
      March 29th, 2011 at 03:32 | #5

      @Christian
      Hi Christian, when compiling for c# 4.0 you can fix the “No constructor for type ‘HideSampleWindowsCommand’ has 0 parameters.” warnings by adding default constructors to the ShowSampleWindowCommand and HideSampleWindowsCommand classes (e.g. “public HideSampleWindowCommand() : base(){}”). However there is at least one other problem with the C# 4.0 version, you can see it by starting the same project, “Open sample Window” then right click the tray icon for the context menu - Options “Show/Hide Context Menu” are permanently greyed out, and the tooltips text is no longer visible (tested under Win XP VS2010 C# V4.0). Still trying to work out why, any tips appreciated, thanks.

    6. March 31st, 2011 at 01:04 | #6

      Keith and others

      I’ll look at theses issues with C# 4.0 asap and post back.

      Cheers,
      Philipp

    7. Alexander Voronin
      April 7th, 2011 at 05:38 | #7

      Hi. I found this topic on CodeProject but since yesterday did not get any suggestions on trigger error. It seems that I misunderstood something in WPF/XAML but I can’t get to work this simply case. I’d like to run trigger by checking context menu attached to notify icon. Sample code yo may find at Your article discussion at CodeProject here - http://www.codeproject.com/Messages/3848077/Re-How-about-triggers-in-ContextMenu.aspx

      I would appreciate any comments and suggestions. And thanx for great work!

    8. macias
      April 26th, 2011 at 00:17 | #8

      Many, many thanks for your library! It helped me a lot. I have one question — you can set animation for you custom balloon, for example as slide. Is there any way to set animation for “closing” the balloon — I would like to slide it down (exactly speaking: to slide it down on timeout, to close it on user manual close). Thank you in advance for figuring it out.

    9. Alexander Voronin
      April 27th, 2011 at 10:14 | #9

      Still no suggestions?

    10. May 10th, 2011 at 02:25 | #10

      Hi

      Very cool library. We use it in Nemo Documents and so far it’s really great. There is just one little problem that we get once in a while, the “Could not create icon data” exception.

      As can be seen here:

      http://msdn.microsoft.com/en-us/library/bb762159%28v=vs.85%29.aspx

      It appears to be related to startup. But I have tried doing a the try and sleep and bit before trying again for a couple of times. This took away quite a bit of them, but there are still some left.

      Looking comments in that microsoft article, maybe it would be a good idea to hook into the WM_CREATE event? That way it would also work if explorer crashed and is restarted.

    11. Ryan
      May 26th, 2011 at 05:33 | #11

      I tried setting the icon for TaskBarIcon

      Sample Code :

      but it gives me this error, I think I already set the icon correctly but still error occurs
      Error : Provide value on ‘System.Windows.Baml2006.TypeConverterMarkupExtension

      Any ideas?

    12. Wietse Sas
      July 13th, 2011 at 03:13 | #12

      Hi,

      I was trying to use the notifyicon for a project i’m working on.

      I came across a bug, to reproduce:
      - End process “explorer.exe”.
      - Start your program which uses the notifyicon (or start your project in visual studio).
      - Start process “explorer.exe”.

      This problem also occurres when a user with a large roaming profile logs on and the program with the notifyicon starts when windows starts. At this time, explorer is not fully started until the user’s profile is loaded.

    13. Phil Jagger
      July 13th, 2011 at 11:34 | #13

      @Chris Baker

      Make sure you’ve set your Application’s ShutdownMode to OnMainWindowClose. By default it’s set to LastWindowClosed and since the notify icon creates a window, it will keep your application alive after the main window closes and you’ll have to kill it from the task manager.

    14. Marcos Lommez
      August 2nd, 2011 at 11:05 | #14

      Hi Philipp,

      I’ve downloaded your WPF NotifyIcon and is great control.
      I’m a new WPF coder, learning the MVVM pattern.
      In my application i’m trying to bind a context menu to a command that i’ve created.
      In a resource file, i’ve created the taskBarIcon and the context menu.

      Here is how, i’m creating the TaskBarIcon and the context menu:

      Here is my base class for commands:

      public class ViewModelCommand : ICommand
      {
      public ViewModelCommand(Action executeAction,
      Predicate canExecute)
      {
      if (executeAction == null)
      throw new ArgumentNullException(”executeAction”);

      _executeAction = executeAction;
      _canExecute = canExecute;
      }

      private readonly Predicate _canExecute;
      public bool CanExecute(object parameter)
      {
      if (_canExecute == null) return true;

      return _canExecute(parameter);
      }

      public event EventHandler CanExecuteChanged;
      public void OnCanExecuteChanged()
      {
      if (CanExecuteChanged != null)
      CanExecuteChanged(this, EventArgs.Empty);
      }

      private readonly Action _executeAction;
      public void Execute(object parameter)
      {
      _executeAction(parameter);
      }
      }

      Here is the viewmodel code for my main window

      public class MainViewModel : ViewModel
      {
      public DataClient dataClient;
      public TaskbarIcon taskBarIcon;

      private ViewModelCommand fCheckSomethingOnServer = null;
      public ViewModelCommand CheckSomethingOnServer
      {
      get
      {
      if (fCheckSomethingOnServer == null)
      {
      fCheckSomethingOnServer = new ViewModelCommand(p => CheckSomething(this, new CheckSomethingEventArgs(dataClient)), p => CanCheckSomething);
      }

      return fCheckSomethingOnServer;
      }
      }

      public class CheckSomethingEventArgs : EventArgs
      {
      public CheckSomethingEventArgs(DataClient dataClient)
      {
      this.dataClient = dataClient;
      }
      public DataClient dataClient { get; set; }
      }

      private void CheckSomething(object sender, CheckSomethingEventArgs e)
      {
      e.DataClient.CheckSomething();
      }

      private bool CanCheckSomething
      {
      get { return dataClient.Logged; }
      }
      }

      Here is the Main Window code:

      public DataClient dataClient;
      private TaskbarIcon tb;
      private MainViewModel _viewModel = null;

      public MainWindow()
      {
      InitializeComponent();
      dataClient = new DataClient();
      tb = (TaskbarIcon)FindResource(”MyNotifyIcon”);
      if (fViewModel == null)
      {
      fViewModel = new MainViewModel();
      fViewModel.dataClient = dataClient;
      fViewModel.taskBarIcon = tb;
      DataContext = fViewModel;
      }
      }

      The problem is that the CheckSomethingOnServer command is never fired.
      Could you help me to fix that problem?
      Feel free to contact my by e-mail.

      Thanks

      Best regards,
      Marcos Lommez

    15. Frank
      August 18th, 2011 at 05:40 | #15

      Wow, I was amazed to the extend of work you put in this assembly. Thank you very much for your efforts. So far, it works fantastic. Big thanks.

    16. August 19th, 2011 at 10:24 | #16

      loving this thing man! THanks! BUilding a timer tool that helps me track multitasking and also add notes to keep track of where i am with all. Lemme know if you’re interested i’ll send it to you - Rich

    17. Marcos Lommez
      August 23rd, 2011 at 02:52 | #17

      Anyone could help me to fix that problem in my previous post?
      Thanks

    18. August 26th, 2011 at 02:45 | #18

      Hi,
      thank you for this awesome tuto and component !
      I tried to add a webbrowser wpf component to the notification but it stays invisible (works when I click on it).
      Do you have any idea to solve this ?
      Regards

    19. August 26th, 2011 at 03:16 | #19

      @Oz

      I found why, this is cause by the AllowsTransparency attributes :
      http://blogs.msdn.com/b/changov/archive/2009/01/19/webbrowser-control-on-transparent-wpf-window.aspx

      Thanks

    20. October 9th, 2011 at 10:36 | #20

      @Wietse Sas

      Did you manage to come up with a solution for this?

    21. Sys
      March 7th, 2012 at 08:41 | #21

      Hi Sumi

      Thank you for this great library. I just have one problem when my program closed, the icon is still in the tray bar. Only when my mouse is over it, then it vanishes. Here’s my code:

      protected override void OnStateChanged(EventArgs e)
      {
      if (WindowState == WindowState.Minimized)
      {
      this.Hide();
      tiNotify.Visibility = System.Windows.Visibility.Visible;
      }
      else tiNotify.Visibility = System.Windows.Visibility.Collapsed;

      base.OnStateChanged(e);
      }

      protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
      {
      //clean up notifyicon (would otherwise stay open until application finishes)
      tiNotify.Dispose();
      base.OnClosing(e);
      }

    Comment pages
    1 2 3 452
    1. May 11th, 2009 at 13:45 | #1
    2. May 26th, 2009 at 18:13 | #2
    3. June 1st, 2009 at 00:52 | #3
    4. June 24th, 2009 at 18:37 | #4
    5. August 4th, 2009 at 06:13 | #5
    6. October 18th, 2009 at 11:00 | #6
    7. October 26th, 2009 at 23:43 | #7
    8. March 11th, 2010 at 17:18 | #8
    9. May 6th, 2010 at 21:18 | #9
    10. May 19th, 2010 at 12:44 | #10
    11. May 27th, 2010 at 14:26 | #11
    12. February 26th, 2011 at 20:07 | #12
    13. September 27th, 2011 at 07:25 | #13