mirror of
https://github.com/ckaczor/wpf-notifyicon.git
synced 2026-01-26 02:45:37 -05:00
WPF NotifyIcon
-------------- CHG Changed show/hide functionality in order to work better with showcase window (which is no longer the app's main window). git-svn-id: https://svn.evolvesoftware.ch/repos/evolve.net/WPF/NotifyIcon@105 9f600761-6f11-4665-b6dc-0185e9171623
This commit is contained in:
@@ -3,6 +3,8 @@ using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Markup;
|
||||
using System.Windows.Media;
|
||||
using Hardcodet.Wpf.TaskbarNotification;
|
||||
|
||||
namespace Samples.Commands
|
||||
{
|
||||
@@ -73,5 +75,84 @@ namespace Samples.Commands
|
||||
.Metadata.DefaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the window that owns the TaskbarIcon class.
|
||||
/// </summary>
|
||||
/// <param name="commandParameter"></param>
|
||||
/// <returns></returns>
|
||||
protected Window GetTaskbarWindow(object commandParameter)
|
||||
{
|
||||
if (IsDesignMode) return null;
|
||||
|
||||
//get the showcase window off the taskbaricon
|
||||
var tb = commandParameter as TaskbarIcon;
|
||||
return tb == null ? null : TryFindParent<Window>(tb);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#region TryFindParent helper
|
||||
|
||||
/// <summary>
|
||||
/// Finds a parent of a given item on the visual tree.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the queried item.</typeparam>
|
||||
/// <param name="child">A direct or indirect child of the
|
||||
/// queried item.</param>
|
||||
/// <returns>The first parent item that matches the submitted
|
||||
/// type parameter. If not matching item can be found, a null
|
||||
/// reference is being returned.</returns>
|
||||
public static T TryFindParent<T>(DependencyObject child)
|
||||
where T : DependencyObject
|
||||
{
|
||||
//get parent item
|
||||
DependencyObject parentObject = GetParentObject(child);
|
||||
|
||||
//we've reached the end of the tree
|
||||
if (parentObject == null) return null;
|
||||
|
||||
//check if the parent matches the type we're looking for
|
||||
T parent = parentObject as T;
|
||||
if (parent != null)
|
||||
{
|
||||
return parent;
|
||||
}
|
||||
else
|
||||
{
|
||||
//use recursion to proceed with next level
|
||||
return TryFindParent<T>(parentObject);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is an alternative to WPF's
|
||||
/// <see cref="VisualTreeHelper.GetParent"/> method, which also
|
||||
/// supports content elements. Keep in mind that for content element,
|
||||
/// this method falls back to the logical tree of the element!
|
||||
/// </summary>
|
||||
/// <param name="child">The item to be processed.</param>
|
||||
/// <returns>The submitted item's parent, if available. Otherwise
|
||||
/// null.</returns>
|
||||
public static DependencyObject GetParentObject(DependencyObject child)
|
||||
{
|
||||
if (child == null) return null;
|
||||
ContentElement contentElement = child as ContentElement;
|
||||
|
||||
if (contentElement != null)
|
||||
{
|
||||
DependencyObject parent = ContentOperations.GetParent(contentElement);
|
||||
if (parent != null) return parent;
|
||||
|
||||
FrameworkContentElement fce = contentElement as FrameworkContentElement;
|
||||
return fce != null ? fce.Parent : null;
|
||||
}
|
||||
|
||||
//if it's not a ContentElement, rely on VisualTreeHelper
|
||||
return VisualTreeHelper.GetParent(child);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,19 +6,20 @@ namespace Samples.Commands
|
||||
/// <summary>
|
||||
/// Hides the main window.
|
||||
/// </summary>
|
||||
public class HideMainWindowCommand : CommandBase<HideMainWindowCommand>
|
||||
public class HideSampleWindowCommand : CommandBase<HideSampleWindowCommand>
|
||||
{
|
||||
|
||||
public override void Execute(object parameter)
|
||||
{
|
||||
Application.Current.MainWindow.Hide();
|
||||
GetTaskbarWindow(parameter).Hide();
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
}
|
||||
|
||||
|
||||
public override bool CanExecute(object parameter)
|
||||
{
|
||||
return !IsDesignMode && Application.Current.MainWindow.IsVisible;
|
||||
Window win = GetTaskbarWindow(parameter);
|
||||
return win != null && win.IsVisible;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace Samples.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Shows the main window.
|
||||
/// </summary>
|
||||
public class ShowMainWindowCommand : CommandBase<ShowMainWindowCommand>
|
||||
{
|
||||
public override void Execute(object parameter)
|
||||
{
|
||||
Application.Current.MainWindow.Show();
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
}
|
||||
|
||||
|
||||
public override bool CanExecute(object parameter)
|
||||
{
|
||||
return !IsDesignMode && Application.Current.MainWindow.IsVisible == false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
24
Source/Sample Project/Commands/ShowSampleWindowCommand.cs
Normal file
24
Source/Sample Project/Commands/ShowSampleWindowCommand.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace Samples.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Shows the main window.
|
||||
/// </summary>
|
||||
public class ShowSampleWindowCommand : CommandBase<ShowSampleWindowCommand>
|
||||
{
|
||||
public override void Execute(object parameter)
|
||||
{
|
||||
GetTaskbarWindow(parameter).Show();
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
}
|
||||
|
||||
|
||||
public override bool CanExecute(object parameter)
|
||||
{
|
||||
Window win = GetTaskbarWindow(parameter);
|
||||
return win != null && !win.IsVisible;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user