Search code examples
wpfnotifyicon

WPF NotifyIcon Reference the TaskbarIcon (Windowless)


I’m new to WPF NotifyIcon and I’m trying to use the Windowless Sample which uses a ResourceDictionary instead of a window and the TaskbarIcon.DataContext is set to my ViewModel. I can call the example commands (ShowWindowCommand, etc.) and it works fine.

However, in my ViewModel I can’t figure out how to reference the TaskbarIcon. I want to show a standard balloon something like NotifyIcon.ShowBallonTip(title, text, BalloonIcon.Error). I’ve tried giving the tb:TaskbarIcon an x:Name but my ViewModel still does not see it.

How do I reference the TaskbarIcon from my ViewModel? Thanks!

 <tb:TaskbarIcon x:Key="NotifyIcon"
                IconSource="/Red.ico"
                ToolTipText="Double-click for window, right-click for menu"
                DoubleClickCommand="{Binding ShowWindowCommand}"
                ContextMenu="{StaticResource SysTrayMenu}">

    <tb:TaskbarIcon.DataContext>
        <local:NotifyIconViewModel />
    </tb:TaskbarIcon.DataContext>
</tb:TaskbarIcon>

Solution

  • I'm not sure if this is the preferred way to accomplish this in a ViewModel but this is how I got a reference to the TaskbarIcon.

    tb = (TaskbarIcon)Application.Current.FindResource("MyTray");