【发布时间】:2018-09-16 18:21:42
【问题描述】:
我在用户控件中定义了一个 KeyDown 事件。目前,当您按 Enter 时它可以工作。我希望它还可以处理 ctrl + v 组合键,以便粘贴复制的文本并执行一些操作。 说明:我的 KeyDown 事件处理程序接收到一个按下的键,如果是 Enter,则获取该控件中的当前焦点(例如,焦点在某个 textBox 上),检查 TextBox "X" 中是否有任何数据,如果有,另一个 TextBox 是自动完成的。而且我需要在文本框“X”中插入文本时,自动完成文本框的其余部分。如何强制程序执行插入并执行我的自动完成代码?
private async void OnKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key != VirtualKey.Enter) return;
var focusedElement = FocusManager.GetFocusedElement();
if (focusedElement == LocationName || focusedElement == AddressTextBox ||
focusedElement == Latitude || focusedElement == Longitude)
{
e.Handled = true;
}
if (IsAddressTextValid)
{
var mapLocation = await FindFirstMapLocationAsync(AddressTextBox.Text);
if (mapLocation != null)
{
LatitudeText = mapLocation.Point.Position.Latitude.ToString();
LongitudeText = mapLocation.Point.Position.Longitude.ToString();
}
}
}
【问题讨论】:
-
这也应该通过鼠标右键下拉菜单的“插入”动作来实现
-
为什么不订阅 TextChanged 事件?
-
因为在这个文本框中手动输入文本时,会不断调用这个事件
标签: c# .net uwp caliburn.micro