### Windowless Application Setup with ResourceDictionary
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
For tray-only applications, declare the TaskbarIcon in a ResourceDictionary and load it in App.xaml. This example shows how to merge the resource dictionary and define the TaskbarIcon with a context menu and commands.
```xml
```
```xml
```
--------------------------------
### App.xaml.cs for Windowless Application
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
In the App.xaml.cs file for a windowless application, find the TaskbarIcon resource on startup and dispose of it on application exit.
```csharp
public partial class App : Application
{
private TaskbarIcon _notifyIcon;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
_notifyIcon = (TaskbarIcon)FindResource("NotifyIcon");
}
protected override void OnExit(ExitEventArgs e)
{
_notifyIcon.Dispose();
base.OnExit(e);
}
}
```
--------------------------------
### Manage WPF NotifyIcon Visibility and State
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Use `Visibility.Collapsed` to hide the tray icon and `Visibility.Visible` to show it. Check `IsTaskbarIconCreated` for its active state and `SupportsCustomToolTips` for platform capabilities. Always call `Dispose()` to clean up resources.
```csharp
// Hide the tray icon (removes it from the taskbar notification area)
MyNotifyIcon.Visibility = Visibility.Collapsed;
// Show it again
MyNotifyIcon.Visibility = Visibility.Visible;
// Check state
if (MyNotifyIcon.IsTaskbarIconCreated)
Console.WriteLine("Icon is active in the tray");
if (MyNotifyIcon.SupportsCustomToolTips)
Console.WriteLine("Running on Vista+, custom tooltips are supported");
// Clean up — automatically called on Application.Exit, but explicit is cleaner
MyNotifyIcon.Dispose();
// After disposal
Console.WriteLine(MyNotifyIcon.IsDisposed); // true
```
--------------------------------
### Setting IconSource and IconFrameworkElementSource
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Configure the tray icon using IconSource for standard image sources or IconFrameworkElementSource for dynamic WPF elements. Mutually exclusive properties.
```xml
```
```xml
```
```xml
```
--------------------------------
### Custom WPF Tooltip with TrayToolTip
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Utilize TrayToolTip to display a rich, styled WPF UIElement as a tooltip. Fallback to ToolTipText for older systems.
```xml
```
--------------------------------
### Interactive Popup with TrayPopup
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Configure TrayPopup to show an interactive UIElement near the tray icon on mouse click. Control activation and placement.
```xml
```
--------------------------------
### Handle Mouse Interactions on the Tray Icon
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Subscribe to routed events for mouse interactions on the TaskbarIcon in XAML or code-behind. Preview events allow for cancellation.
```xml
```
```csharp
private void OnTrayLeftMouseDown(object sender, RoutedEventArgs e)
=> Debug.WriteLine("Left button pressed on tray icon");
private void OnTrayDoubleClick(object sender, RoutedEventArgs e)
{
Application.Current.MainWindow?.Show();
Application.Current.MainWindow?.Activate();
}
// Preview (tunneling) events allow cancellation before the action occurs
MyNotifyIcon.PreviewTrayContextMenuOpen += (s, e) =>
{
if (ShouldSuppressMenu())
e.Handled = true; // Prevents context menu from opening
};
```
--------------------------------
### Programmatically Changing IconSource or Assigning System.Drawing.Icon
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Dynamically update the tray icon at runtime using BitmapImage or a System.Drawing.Icon.
```csharp
MyNotifyIcon.IconSource = new BitmapImage(new Uri("pack://application:,,,/Icons/Active.ico"));
```
```csharp
MyNotifyIcon.Icon = new System.Drawing.Icon("path/to/icon.ico");
```
--------------------------------
### Programmatically Controlling TrayPopup
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Show or close the TrayPopup using its methods.
```csharp
MyNotifyIcon.ShowTrayPopup();
MyNotifyIcon.CloseTrayPopup();
```
--------------------------------
### Basic XAML Declaration for TaskbarIcon
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Declare the TaskbarIcon directly in XAML for a simple system tray icon. Ensure to dispose of the icon when the window closes to prevent resource leaks.
```xml
```
--------------------------------
### Bind Commands to Tray Icon Clicks
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Bind ICommand objects to left-click and double-click interactions on the tray icon. Supports custom commands and routed WPF commands.
```xml
```
```csharp
// ViewModel-based MVVM command binding (commands exposed as ICommand properties)
// NotifyIconResources.xaml binds to ViewModel directly:
// DoubleClickCommand="{Binding ShowWindowCommand}"
public class NotifyIconViewModel : INotifyPropertyChanged
{
public ICommand ShowWindowCommand => new RelayCommand(_ =>
{
Application.Current.MainWindow ??= new MainWindow();
Application.Current.MainWindow.Show();
Application.Current.MainWindow.Activate();
});
public ICommand HideWindowCommand => new RelayCommand(
_ => Application.Current.MainWindow?.Hide(),
_ => Application.Current.MainWindow?.IsVisible == true);
public ICommand ExitApplicationCommand => new RelayCommand(
_ => Application.Current.Shutdown());
}
```
--------------------------------
### Show Balloon Tip with Standard Icons
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Displays a standard Windows balloon notification with a title, message, and severity icon. Use BalloonIcon enum for standard icons.
```csharp
// Show a built-in balloon with a standard icon (Info, Warning, Error, None)
MyNotifyIcon.ShowBalloonTip("Update Available",
"Version 2.1 is ready to install.",
BalloonIcon.Info);
```
```csharp
// Show a balloon with a custom System.Drawing.Icon
var customIcon = new System.Drawing.Icon("path/to/custom.ico");
MyNotifyIcon.ShowBalloonTip("Custom Notification",
"Something happened!",
customIcon,
largeIcon: true); // Windows Vista+ large icon
```
--------------------------------
### Handle Balloon Tip Events
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Subscribes to events related to the balloon notification's lifecycle, such as showing, closing, and clicking.
```csharp
// React to balloon events
MyNotifyIcon.TrayBalloonTipShown += (s, e) => Console.WriteLine("Balloon shown");
MyNotifyIcon.TrayBalloonTipClosed += (s, e) => Console.WriteLine("Balloon closed");
MyNotifyIcon.TrayBalloonTipClicked += (s, e) => Console.WriteLine("Balloon clicked");
```
--------------------------------
### ShowBalloonTip
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Displays a standard Windows balloon notification with a title, message, and severity icon. It can also display a custom System.Drawing.Icon. The balloon can be programmatically dismissed using HideBalloonTip(). Event handlers for TrayBalloonTipShown, TrayBalloonTipClosed, and TrayBalloonTipClicked are also provided.
```APIDOC
## ShowBalloonTip
Displays a standard Windows balloon notification with a title, message, and severity icon.
### Method
```csharp
MyNotifyIcon.ShowBalloonTip(string title, string message, BalloonIcon icon)
MyNotifyIcon.ShowBalloonTip(string title, string message, System.Drawing.Icon icon, bool largeIcon)
```
### Parameters
#### ShowBalloonTip(string, string, BalloonIcon)
- **title** (string) - The title of the balloon notification.
- **message** (string) - The main text content of the balloon notification.
- **icon** (BalloonIcon) - The built-in icon to display (Info, Warning, Error, None).
#### ShowBalloonTip(string, string, System.Drawing.Icon, bool)
- **title** (string) - The title of the balloon notification.
- **message** (string) - The main text content of the balloon notification.
- **icon** (System.Drawing.Icon) - A custom System.Drawing.Icon for the balloon.
- **largeIcon** (bool) - Whether to display the icon in a larger format (Windows Vista+).
### Endpoint
N/A (Method Call)
### Request Example
```csharp
// Show a built-in balloon with a standard icon
MyNotifyIcon.ShowBalloonTip("Update Available",
"Version 2.1 is ready to install.",
BalloonIcon.Info);
// Show a balloon with a custom System.Drawing.Icon
var customIcon = new System.Drawing.Icon("path/to/custom.ico");
MyNotifyIcon.ShowBalloonTip("Custom Notification",
"Something happened!",
customIcon,
largeIcon: true);
```
### Response
N/A
### Related Methods
- **HideBalloonTip()**: Hides the currently displayed balloon notification.
- **TrayBalloonTipShown**: Event that fires when the balloon is shown.
- **TrayBalloonTipClosed**: Event that fires when the balloon is closed.
- **TrayBalloonTipClicked**: Event that fires when the balloon is clicked.
```
--------------------------------
### Manage Custom Balloon Timer and Closing
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Provides methods to reset the auto-close timer for a custom balloon or to close it manually.
```csharp
// Reset the auto-close timer (e.g. on mouse enter)
MyNotifyIcon.ResetBalloonCloseTimer();
```
```csharp
// Close the balloon manually
MyNotifyIcon.CloseBalloon();
```
--------------------------------
### PopupActivation and MenuActivation Modes
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Control which mouse interactions trigger the TrayPopup or ContextMenu using the PopupActivationMode enum.
```xml
```
--------------------------------
### ShowCustomBalloon
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Displays any UIElement as a custom balloon near the tray area. It supports optional PopupAnimation and an auto-close timeout. The balloon can receive BalloonShowing and BalloonClosing attached events for animations. It can be closed manually or automatically after a specified timeout.
```APIDOC
## ShowCustomBalloon
Displays any `UIElement` as a fully custom balloon near the tray area, with optional `PopupAnimation` and an auto-close timeout.
### Method
```csharp
MyNotifyIcon.ShowCustomBalloon(UIElement balloonContent, PopupAnimation animation, int? timeout)
```
### Parameters
- **balloonContent** (UIElement) - The UI element to display as the balloon.
- **animation** (PopupAnimation) - The animation to use when showing the balloon (e.g., Slide, Fade).
- **timeout** (int? | null) - The auto-close timeout in milliseconds. If null, the balloon will not auto-close.
### Endpoint
N/A (Method Call)
### Request Example
```csharp
// Show the custom balloon with Slide animation, auto-closes after 4 seconds
var balloon = new FancyBalloon { BalloonText = "Download complete!" };
MyNotifyIcon.ShowCustomBalloon(balloon, PopupAnimation.Slide, timeout: 4000);
// Show the custom balloon with Fade animation, stays open indefinitely
MyNotifyIcon.ShowCustomBalloon(balloon, PopupAnimation.Fade, timeout: null);
```
### Response
N/A
### Related Methods
- **ResetBalloonCloseTimer()**: Resets the auto-close timer for the currently displayed custom balloon.
- **CloseBalloon()**: Closes the currently displayed custom balloon manually.
### Associated Events
- **TaskbarIcon.BalloonShowing**: Routed event that fires when the balloon popup opens, can be used to trigger animations.
- **TaskbarIcon.BalloonClosing**: Routed event that fires before the balloon is closed, can be used to cancel the close or trigger animations.
```
--------------------------------
### Bind to Owning TaskbarIcon via Attached Property
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Use the `ParentTaskbarIcon` attached property to bind child controls (like tooltips) back to the owning `TaskbarIcon` instance. This is useful for accessing properties like `ToolTipText` from within nested elements.
```xml
```
--------------------------------
### Custom Popup Placement Delegate
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Override the default tray-area placement of popups by providing a delegate to the `CustomPopupPosition` property. This delegate should return the desired screen coordinates for the popup. You can also retrieve the current tray position using `GetPopupTrayPosition`.
```csharp
// Position the custom balloon at the center of the primary screen
MyNotifyIcon.CustomPopupPosition = () =>
{
var screen = System.Windows.SystemParameters.PrimaryScreenWidth;
var screenH = System.Windows.SystemParameters.PrimaryScreenHeight;
return new Hardcodet.Wpf.TaskbarNotification.Interop.Point
{
X = (int)(screen / 2) - 120,
Y = (int)(screenH / 2) - 60
};
};
var balloon = new FancyBalloon { BalloonText = "Centered balloon!" };
MyNotifyIcon.ShowCustomBalloon(balloon, PopupAnimation.Fade, 3000);
// Get the current tray location without overriding
var trayPosition = MyNotifyIcon.GetPopupTrayPosition();
Console.WriteLine($"Tray icon is at {trayPosition.X}, {trayPosition.Y}");
```
--------------------------------
### MVVM Integration with TaskbarIcon DataContext
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
The `TaskbarIcon` automatically propagates its `DataContext` to child controls like popups and context menus. Assigning a ViewModel to the `TaskbarIcon` enables transparent data binding throughout these elements.
```xml
```
```csharp
public class AppViewModel : INotifyPropertyChanged
{
private string _statusMessage = "Ready";
public string StatusMessage
{
get => _statusMessage;
set { _statusMessage = value; OnPropertyChanged(nameof(StatusMessage)); }
}
public ICommand RefreshCommand => new RelayCommand(_ => StatusMessage = $"Refreshed at {DateTime.Now:T}");
public ICommand ExitCommand => new RelayCommand(_ => Application.Current.Shutdown());
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
```
--------------------------------
### Dispose TaskbarIcon in Window Closing Event
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
It is crucial to dispose of the TaskbarIcon instance when the window is closing to properly release system resources.
```csharp
protected override void OnClosing(CancelEventArgs e)
{
MyNotifyIcon.Dispose();
base.OnClosing(e);
}
```
--------------------------------
### Show Custom Balloon Notification
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Displays a custom UI element as a balloon notification near the tray area. Supports animations and auto-close timers.
```csharp
// Show the custom balloon — auto-closes after 4 seconds
var balloon = new FancyBalloon { BalloonText = "Download complete!" };
MyNotifyIcon.ShowCustomBalloon(balloon, PopupAnimation.Slide, timeout: 4000);
```
```csharp
// Keep the balloon open indefinitely (pass null timeout)
MyNotifyIcon.ShowCustomBalloon(balloon, PopupAnimation.Fade, timeout: null);
```
--------------------------------
### XAML Declaration for TaskbarIcon
Source: https://github.com/hardcodet/wpf-notifyicon/blob/develop/README.md
Declare the TaskbarIcon control in XAML to integrate it into your WPF application. Configure properties like IconSource, ToolTipText, and activation behaviors.
```XML
```
--------------------------------
### Hide Balloon Tip
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Programmatically dismisses the currently displayed balloon notification.
```csharp
// Hide the currently displayed balloon
MyNotifyIcon.HideBalloonTip();
```
--------------------------------
### WPF Custom Balloon XAML
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Defines a custom WPF UI element (UserControl) to be used as a balloon notification. Includes animations for showing and closing.
```xml
```
--------------------------------
### Cancel Auto-Close for Balloon Animations
Source: https://context7.com/hardcodet/wpf-notifyicon/llms.txt
Handle the BalloonClosing routed event to prevent immediate closure, allowing animations to play. This is typically done in the code-behind of a custom balloon control.
```csharp
// FancyBalloon.xaml.cs
public partial class FancyBalloon : UserControl
{
public string BalloonText
{
get => (string)GetValue(BalloonTextProperty);
set => SetValue(BalloonTextProperty, value);
}
public static readonly DependencyProperty BalloonTextProperty =
DependencyProperty.Register(nameof(BalloonText), typeof(string), typeof(FancyBalloon));
// Called when the FadeOut storyboard completes — actually close the balloon
private void OnFadeOutCompleted(object sender, EventArgs e)
{
// Retrieve the parent TaskbarIcon via the inherited attached property
Popup popup = Parent as Popup;
if (popup != null) popup.IsOpen = false;
}
// Handle BalloonClosing to keep the popup open during the fade-out animation
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
TaskbarIcon.AddBalloonClosingHandler(this, OnBalloonClosing);
}
private void OnBalloonClosing(object sender, RoutedEventArgs e)
{
// Mark as handled → TaskbarIcon will not close the popup immediately
e.Handled = true;
// The FadeOut storyboard (triggered above) will close it when finished
}
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.