public static MyTT Create(UIElement content) { MyTT wnd = new MyTT(); wnd.Content = content; wnd.WindowStartupLocation = WindowStartupLocation.Manual; wnd.WindowState = WindowState.Normal; wnd.WindowStyle = WindowStyle.None; wnd.ShowInTaskbar = false; wnd.SizeToContent = SizeToContent.WidthAndHeight; wnd.AllowsTransparency = true; wnd.Background = System.Windows.Media.Brushes.Transparent; wnd.Topmost = true; var b = new Binding("DataContext") { Source = Application.Current.MainWindow }; BindingOperations.SetBinding(wnd, DataContextProperty, b); wnd.MouseEnter += wnd.OnMouseEnter; wnd.MouseLeave += (sender, args) => { wnd._gotInside = false; wnd.IsOpen = false; }; wnd.LostFocus += (sender, args) => { wnd.IsOpen = false; }; wnd.IsVisibleChanged += (sender, args) => wnd.IsOpen = wnd.Visibility == Visibility.Visible; wnd.SourceInitialized += (sender, args) => { HwndSource src = (HwndSource)HwndSource.FromVisual(wnd); src.AddHook(wnd.WndProc); }; return(wnd); }
/// <summary> /// Creates a <see cref="ToolTip"/> control that either /// wraps the currently set <see cref="TrayToolTip"/> /// control or the <see cref="ToolTipText"/> string.<br/> /// If <see cref="TrayToolTip"/> itself is already /// a <see cref="ToolTip"/> instance, it will be used directly. /// </summary> /// <remarks>We use a <see cref="ToolTip"/> rather than /// <see cref="Popup"/> because there was no way to prevent a /// popup from causing cyclic open/close commands if it was /// placed under the mouse. ToolTip internally uses a Popup of /// its own, but takes advance of Popup's internal <see cref="Popup.HitTestable"/> /// property which prevents this issue.</remarks> private void CreateCustomToolTip() { //check if the item itself is a tooltip FrameworkElement tt = TrayToolTip as MyTT; if (tt == null && TrayToolTip != null) { //create an invisible tooltip that hosts the UIElement tt = MyTT.Create(TrayToolTip); //tt.Placement = PlacementMode.Mouse; //do *not* set the placement target, as it causes the popup to become hidden if the //TaskbarIcon's parent is hidden, too. At runtime, the parent can be resolved through //the ParentTaskbarIcon attached dependency property: //tt.PlacementTarget = this; //make sure the tooltip is invisible //tt.HasDropShadow = false; (tt as MyTT).BorderThickness = new Thickness(0); (tt as MyTT).Background = System.Windows.Media.Brushes.Transparent; } else if (tt == null && !String.IsNullOrEmpty(ToolTipText)) { //create a simple tooltip for the string tt = new ToolTip(); var b = new Binding("DataContext") { Source = this }; BindingOperations.SetBinding(tt, DataContextProperty, b); (tt as ToolTip).Content = ToolTipText; } //the tooltip explicitly gets the DataContext of this instance. //If there is no DataContext, the TaskbarIcon assigns itself if (tt != null) { UpdateDataContext(tt, null, DataContext); } //store a reference to the used tooltip SetTrayToolTipResolved(tt); }