I have a wpf application that is using a NavigationWindow that allows me to load pages within the MasterWindow.
I am trying to add the notifyicon feature http://www.codeproject.com/KB/WPF/wpf_notifyicon.aspx to my application that will allow me to display a notification window when I want.
I have added the taskbaritem to my navigationwindow
<tb:TaskbarIcon x:Name="MyNotifyIcon" ToolTipText="My Application" IconSource="/Images/Icons/TB.ico"/>
I want to be able to create a helper class that can access MyNotifyIcon from any of the pages loaded. I was thinking something like
public static void DisplayMessageArea(string messageToDisplay)
{
var balloon = new StandardNotification {BalloonMessage = messageToDisplay };
//TaskbarIcon tb = (TaskbarIcon)MasterWindow.Resources.FindName("MyNotifyIcon");
//tb.ShowCustomBalloon(balloon,PopupAnimation.Slide,4000);
}
However I don’t know the correct way to find the controller “MyNotifyIcon” from my helper class.
Can someone point me in the right direction?
I’ve never seen that NotifyIcon before (kinda cool, by the way), but I took a look at its API. What I would suggest is that you not bother naming that control and trying to find it in your code. Instead, in your main navigation window, set its datacontext to a helper class that you define, and bind to its iconsource, tooltiptext, etc:
(More about value converters here).
Now, the “SomeViewModel” class will implement INotifyPropertyChanged and expose properties like:
…except that these properties will have actual getters/setters and raise the property changed event.
With this approach, you don’t need to tell your presentation layer to go looking in your View layer, which isn’t really ideal. It’s better if your view (XAML) knows about your presentation (code), but not vice-versa, as this loosens coupling. In this case, it loosens coupling by virtue of the fact that your actual code does not depend on the existence of some named control declared in XAML.
As to how to get at this helper view model, you can pass a reference to it around to the various classes that can set it, or you can have the sub-controls of the navigation window raise events that the nav window listens for, or you can go with the approach that you had in mind, which is to define a static method on the view model (I’m not an advocate of this approach, personally, but it’s the closest to what you’re looking to do).
If you’re set on the approach that you’re taking here, however, keep in mind that you do have it named in that control, so you can re-expose it as a static property on the control in question:
Now, you can access it from your helper class:
I would not personally advocate that approach at all, but it is probably the easiest way to do, specifically, what you’re asking. If you go this route, however, remember to check the TaskBarIcon static property for null before doing anything with it, and keep in mind that the static property will work return a value whether or not your control containing it has been loaded or even instantiated.