110,533
社区成员
发帖
与我相关
我的任务
分享
public MainWindow()
{
InitializeComponent();
var timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 1) };
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
if (!acp.IsFocused)
{
acp.Focus();
keybd_event(0xD, 0, 0, 0);
}
}
如果正在输入的话,会导致框内容被选中,并覆盖。[DllImport("USER32.DLL")]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); //导入模拟键盘的方法
private void button1_Click(object sender, RoutedEventArgs e)
{
acp.Focus();
keybd_event(0xD, 0, 0, 0);
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
Title="MainWindow" Height="350" Width="525" FontSize="14">
<Grid>
<input:AutoCompleteBox Name="acp" IsTabStop="True" IsEnabled="True" Margin="40,136,29,145" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="40,76,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
acp.Focus();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
acp.Focus();
}
}
}