如何在全屏模式下使用Windows10上的WPF应用程序并打开键盘(TabTip)时将当前(聚焦)控件滚动到视图中?
从TabTip获取窗口句柄并获取有关窗口可见性和窗口高度的信息,以调整我的WPF窗口高度,以便在键盘可见时移动alle内容。 这工作于10.0.14393,直到Windows更新。
使用WPFTabTip自动键盘,当点击控件但不将控件滚动到视图中时会导致键盘频繁闪烁
答案 0 :(得分:0)
我也面临着同样的问题,这是我的解决方案:
VirtualKeyboardHelper.cs
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
namespace YourNamespace
{
/// <summary>
/// Adjust parent control when child control got focus and virtual keyboard shown
/// </summary>
public class VirtualKeyboardHelper
{
bool _isParentAdjusted = false;
const int DefaultShiftSize = 300;
public int ShiftSize { get; set; } = DefaultShiftSize;
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(String sClassName, String sAppName);
static bool IsVirtualKeyboardShown()
{
var iHandle = (int)FindWindow("IPTIP_Main_Window", "");
return (iHandle > 0);
}
private void DoSetAdjustmentOnFocus(UserControl owner, UIElement child)
{
child.GotFocus += delegate
{
var m = owner.Margin;
if (IsVirtualKeyboardShown())
{
owner.Margin = new Thickness(m.Left, m.Top - ShiftSize, m.Right, m.Bottom);
_isParentAdjusted = true;
}
};
child.LostFocus += delegate
{
var m = owner.Margin;
if (_isParentAdjusted)
owner.Margin = new Thickness(m.Left, m.Top + ShiftSize, m.Right, m.Bottom);
};
}
public static void SetAdjustmentOnFocus(UserControl owner, UIElement child, int shiftSize = DefaultShiftSize)
=> new VirtualKeyboardHelper() { ShiftSize = shiftSize }.DoSetAdjustmentOnFocus(owner, child);
public static void SetAdjustmentOnFocus(UserControl owner, List<UIElement> children)
{
foreach (var c in children)
SetAdjustmentOnFocus(owner, c);
}
}
}
MyControl.cs
public partial class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
// _textCity - is a TextBox defined in XAML
VirtualKeyboardHelper.SetAdjustmentOnFocus(this, _textCity, 400);
VirtualKeyboardHelper.SetAdjustmentOnFocus(this, _textState);
}
}
可以通过实现attched属性以在XAML中使用它来改进此解决方案。