Modal Window: Tooltip does not appear until window is reactivated

17 hours ago 3
ARTICLE AD BOX

I am creating a modal window in WinUI 3, using the sample from the "AppWindow" section in WinUI 3 Gallery as a reference. Inside this modal window, I want to display a tooltip when hovering over a control. However, the tooltip does not appear when I hover the mouse immediately after opening the modal window. If I deactivate the window (e.g., by clicking outside) and then reactivate it by clicking on the window again, the tooltip starts to appear as expected. How can I make the tooltip appear right after opening the modal window, without needing to reactivate the window?

Below is a simplified version of my code:

<Window x:Class="ModalWindowTest.ModalWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ModalWindow"> <Grid> <TextBlock Text="Text to display" ToolTipService.ToolTip="Tooltip content" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16"/> </Grid> </Window>

xaml.cs

using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using System; using System.Runtime.InteropServices; using WinRT.Interop; namespace ModalWindowTest { public sealed partial class ModalWindow : Window { private readonly Window _parent; public ModalWindow( Window parent ) { InitializeComponent(); _parent = parent; this.SetWindowOwner( _parent ); OverlappedPresenter presenter = OverlappedPresenter.CreateForDialog(); presenter.IsResizable = true; presenter.IsMinimizable = false; presenter.IsMaximizable = false; presenter.IsModal = true; this.AppWindow.Resize( new( 300, 300 ) ); this.AppWindow.SetPresenter( presenter ); this.AppWindow.Show(); } private void SetWindowOwner( Window owner ) { IntPtr ownerHwnd = WindowNative.GetWindowHandle( owner ); IntPtr ownedHwnd = Win32Interop.GetWindowFromWindowId( this.AppWindow.Id ); if( IntPtr.Size == 8 ) SetWindowLongPtr( ownedHwnd, -8, ownerHwnd ); else SetWindowLong( ownedHwnd, -8, ownerHwnd ); } [DllImport( "user32.dll", CharSet = CharSet.Auto, EntryPoint = "SetWindowLongPtr" )] public static extern IntPtr SetWindowLongPtr( IntPtr hWnd, Int32 nIndex, IntPtr dwNewLong ); [DllImport( "user32.dll", CharSet = CharSet.Auto, EntryPoint = "SetWindowLong" )] public static extern IntPtr SetWindowLong( IntPtr hWnd, Int32 nIndex, IntPtr dwNewLong ); } }
Read Entire Article