我的 WPF 窗口有一个Button
和一个ListView
. 在Button
的点击事件上,它具有以下代码。
private void myButton1_Click(object sender, RoutedEventArgs e)
{
ListViewItem lvi = new ListViewItem()
{
Content = "Hello",
Focusable = true,
IsEnabled = true
};
this.listView1.Items.Add(lvi);
lvi.Focus();
}
lvi(ListViewItem)
这里的问题是,用户点击后焦点无法移动到Button
。代码lvi.Focus()
没有任何作用。有人可以告诉我为什么会发生这种情况,我该如何解决?
更新:
找到解决方案。调用此代码,否则焦点仍被按钮捕获。
this.Dispatcher.BeginInvoke(new Action(() => lvi.Focus()), System.Windows.Threading.DispatcherPriority.Input);