WPF中Popup上的textbox无法切换到中文输入法

As Marco Zhou has said in the msdn forum (http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b2428b85-adc9-4a1e-a588-8dbb3b9aac06/):
Windows will only send WM_IME_SETCONTEXT message to the active window, Popup by default is designed to be shown with WM_EX_NOACTIVE which means that it doesn't in active state when displaying, that's why IME could not work correctly in this regard, to workaround this issue, you could try set the Popup as the active window using win32 SetActiveWindow() API.

So the workaround is use MyPopup instead of Popup:

public class MyPopup : Popup
{
    [DllImport("user32.dll")]
    static extern IntPtr SetActiveWindow(IntPtr hWnd);

    static MyPopup()
    {
        EventManager.RegisterClassHandler(
            typeof(MyPopup),
            Popup.PreviewGotKeyboardFocusEvent,
            new KeyboardFocusChangedEventHandler(OnPreviewGotKeyboardFocus),
            true);
    }

    private static void OnPreviewGotKeyboardFocus(Object sender, KeyboardFocusChangedEventArgs e)
    {
        var textBox = e.NewFocus as TextBoxBase;
        if (textBox != null)
        {
            var hwndSource = PresentationSource.FromVisual(textBox) as HwndSource;
            if (hwndSource != null)
            {
                SetActiveWindow(hwndSource.Handle);
            }
        }
    }
}

官方解决方案

使用的话自己建一个类代码如上,控件内local:MyPopup 等同于Popup,亲测好使,实用。

参考:https://connect.microsoft.com/VisualStudio/feedback/details/389998/wpf-popup-messes-with-ime-switching

转载于:https://www.cnblogs.com/Tangcy/p/8118481.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值