WPF NotifyIcon
Version 1.0.8 released April 2nd 2016.
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.
Browse/fork/clone on GitHub (includes sample application)
Download library via NuGet
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.
Tutorial and Support
-
-
- A comprehensive tutorial that complements the attached sample application can be found on the Code Project:
-
http://www.codeproject.com/KB/WPF/wpf_notifyicon.aspx
Please post support questions to the CodeProject forum only. Thank you.
Screenshots
The screenshots below were taken from NetDrives and the sample application.
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>
Great stuff.
Can you explain how I use this to do something like:
– user drags files or folders onto the trayicon
– usercontrol pops up and user can drop the files onto the usercontrol
I do not seem to find how to wire up the drag over / drop events …
Are you planning to put this somewhere like codeplex? It would be great as we could then hook into codeplex source control so we can always keep in sync with the latest.
Thanks
David,
No, the control is a part of my own repository, so I’ll keep the sources here. However, if you want to keep up-to-date, you can subscribe to the blog’s feed – I’m announcing all updates here.
Cheers,
Philipp
Hi,
This is great but have you considered how to show multiple balloons? I have a scenario where one baloon may be showing and another baloon is requested?
Hi Phillip,
Just wanted to say congrats on a great piece of code! Thanks for sharing it
Neal
Hi,
Is this sample code availible in vb?
Regards,
@Sam
Sam, there is actually a way to keep the balloon open. Have a look at the CloseBalloon method in the control. You will see that if the BalloonClosing event is being handled (setting the Handled property of the method to true), the popup is *not* being closed.
@Neal Hudson
Thanks for your kind words. Happy coding π
@Jacobus
C# only, I’m afraid. However, you could open the compiled sample in Reflector, which can also decompile the code to VB, I think. For snippets, you could still use a C#->VB converter. There are a few of them on the net where you can just convert C# code on the fly to VB.NET.
@Bart Roelant
Bart,
I would have to find out how to DND onto the tray myself. Keep in mind that the tray icon itself is not a WPF or .NET component – this is pure Windows API. You will have to search the WWW into that direction.
@Philipp Sumi
This sounds like a mission. I think it would be easier just to do the project over in C#.
Thanx for the quick response.
@Jacobus
Just in case you don’t know yet: You can easily use the compiled (C#) assembly in a VB project.
Simple question: how to use the notify icon without putting this into the window.
My use case – only what I have is the application.
@RS
You can easily declare the control in code:
License?
@arj
CodeProject Open License (CPOL)
ContextMenu was used.
And, ContextMenu was used Command.
But ContextMenu was Disabled.
How to use the ContextMenu and Command?
What about memory consumption? Any hints or tricks to keep memory down when the balloons/windows are not being shown? WPF apps consume quite a bit of memory compared to a native tasktray application. Call the garbage collector? call Win32 APIs like SetWorkingSetSizeEx (I may have misremembered the name of the API but you get the idea)
Graham,
I’d leave it up to the runtime to free memory when not needed. If you’re (WPF) app is minimized to the tray and there is no UI living, you shouldn’t really have to worry about memory consumption – unused RAM will be freed if necessary, so in practice, there won’t be any difference to manually running the GC when it comes to system performance.
I am having an issue binding IconSource to a Boolean and using a converter to return my Icons. When I put a break point in my converter it doesn’t break. the Notification Icon shows in the tray but the icon is blank. Anyone have an idea how to fix this?
Thanks,
Dan
Hi, I ran the solution using VS2010 (upgraded the solution file) on Windows 7 64 bit. The standard operating system balloons worked with the standard icons but the custom icon option did not popup.
I noticed this after my program using your library stopped popping up after I moved to windows 7 on my new computer. I have reverted back to a standard system icon in the mean time as a workaround…
Thanks
Hamish,
The sample project runs fine under Win7 x64 with both VS2008 and VS2010. Migrating the project to .NET 4.0 still displayed the correct custom icon on the balloon. Can you verify that the sample project that comes with the control works as expected?
Hi Philipp – I have figured out how you can reproduce this. It happens after you right click desktop -> Personalize -> Display and change the text size to something higher than 100%.
I changed mine back to 100% (from 125%) to test and the popup displayed as usual. To be honest this windows feature has caused the odd bit of software I have to go slightly odd so it’s not just this (majority of things are still OK though). However, with laptops having such high resolutions these days (hence small text), I’m thinking that increasing the text size may be quite a popular option that people do.
Sounds like it could be a possible nightmare to fix though.
Are you God ?
You are amazing thanks for it…
I can’t seem to figure out how to wire up the left click of the Taskbar Icon to a function in my MainWindow class to restore the main window. I got it to link to a seperate class like you have in your example. From there i could create a new main window but thats not what i want to do. Please help.
Thanks a lot for this.
One thing, do you know why the DynamicResource are not working in Taskbar? It works but it didn’t update when the resource is changed. I was just trying to change the text on Taskbar context menu.
Thanks again. π
Great thanks.
This is a amazing control!
Thanks so much for your work on this! It is fabulous! π
Awesome. Great work.
Great job with this! One extremely minor note: In NotifyIconData.cs setting the fixed timeout to 10 has no effect since you’re immediately set it again a few lines down π
Nicely spotted, Shane! I’ll have to look into that, this might actually be an indicator for a bug.
Thanks for telling (and your kind words, of course) π
Awesome work, thanks a lot!
I only have an issue with the standard balloon tips: They don’t show up on Windows XP SP3. Aren’t they supported on XP?
Update: It didn’t work only on one specific Windows XP machine. Tried another one today and found it working perfectly. Forget about it, seems to be a problem with one of my machines.
Awesome work, thanks a lot!
How can i play a sound when the Popup is displayed..Is it Possible?
@Priji John
Probably the simplest solution is to just register an event listener for the event that fires if the popup is being opened (TrayPopupOpen) and trigger the sound from code. As an alternative, you could also use the attached event that is fired for your popup control and trigger the sound via a behavior.
Could you please make a vb.net version like for windows forms.
A small problem I have with your script, it’s almost like focus doesn’t register as I added a textbox inside a TrayPopup and when you type nothing happens. Even the KeyDown event on the textbox doesn’t fire. Any advice?
Hi Philipp! Greate work! But there another one extremely minor note:
Commands dosn’t work when Commands in the ContextMenu, exact ContextMenu items are disabled when Commands is used. Jang already has same issue.
Did you remove this resource http://www.hardcodet.net/taskbar?
My XAML will no longer load in the designer.
The error is
The type ‘tb:TaskbarIcon’ was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
Thank you,
Chris
Great job! Just an issue running your sample project:
In “Sample Project” -> “Sample windows”-> items in context menu are disabled (gray color).
I’m using VCS2010 Express and I have convert solution to Framework 4.0 and building solution doesn’t give any error. I just found in NotifyIconResourse.xaml: Command=”{Commands:ShowSampleWindowCommand}” and Command=”{Commands:HideSampleWindowCommand}” underlined with this advice: “No constructor for type ‘HideSampleWindowsCommand’ has 0 parameters.”
where’s the problem??
Thank you!
Hi, first of all nice library! But I have a problem with the context menu of the TaskbarIcon. I added some entries to the context menu and tried to use a command like ApplicationCommands.Close on one of these menu items. At runtime the menu item witch is bound to the command is all time disabled. What is wrong?
@Victor Same here (problem with Commands and ContextMenu’s). Great library though, thanks for sharing!
Are you aware of HwndSource in System.Windows.Interop? It would simplify some of your code.
Nathan,
I am :). What kind of simplification are you suggesting?
Same problem with Daub.What a pity!
Thanks anyway.