WPF中的Popup控件挡住了输入法
WPF中的Popup控件挡住了输入法,但我的输入焦点是在Popup里的一个TextBox上啊。大家帮忙看看怎么解决啊
[解决办法]
WPF中自带的Popup作为弹出对话框确实有这个问题,你可以自己写一个popup,让他只在应用程序上面,不要在所有窗体最上面,网上有现成的代码:
public class PopupNonTopmost : Popup{ public static DependencyProperty TopmostProperty = Window.TopmostProperty.AddOwner( typeof( PopupNonTopmost ), new FrameworkPropertyMetadata( false, OnTopmostChanged ) ); public bool Topmost { get { return (bool)GetValue( TopmostProperty ); } set { SetValue( TopmostProperty, value ); } } private static void OnTopmostChanged( DependencyObject obj, DependencyPropertyChangedEventArgs e ) { ( obj as PopupNonTopmost ).UpdateWindow(); } protected override void OnOpened( EventArgs e ) { UpdateWindow(); } private void UpdateWindow() { var hwnd = ( (HwndSource)PresentationSource.FromVisual( this.Child ) ).Handle; RECT rect; if ( GetWindowRect( hwnd, out rect ) ) { SetWindowPos( hwnd, Topmost ? -1 : -2, rect.Left, rect.Top, (int)this.Width, (int)this.Height, 0 ); } } #region P/Invoke imports & definitions [StructLayout( LayoutKind.Sequential )] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } [DllImport( "user32.dll" )] [return: MarshalAs( UnmanagedType.Bool )] private static extern bool GetWindowRect( IntPtr hWnd, out RECT lpRect ); [DllImport( "user32", EntryPoint = "SetWindowPos" )] private static extern int SetWindowPos( IntPtr hWnd, int hwndInsertAfter, int x, int y, int cx, int cy, int wFlags ); #endregion}