wpf - 在作为 ListBoxItem 内容的 UserControl 上的 TextBox 上设置焦点
问题描述
我有一个Button
在 aUserControl
上添加一个项目到 a ListBox
on that UserControl
。我们称该控件为父控件。sListBoxItem
包含另一个. UserControl
我们就叫那个孩子吧。该按钮将一个项目添加到ItemSource
列表框(MVVM 样式)。
我可以毫无问题地将其滚动到视图中。我可以将焦点设置为ListBoxItem
,但我想要的是将焦点TextBox
设置UserControl
在ListBoxItem
. 我似乎无法弄清楚。下面的代码将焦点设置为ListBoxItem
,而不是它的UserControl
子级或它上的任何控件。
Private Sub bnAdd(sender As Object, e As RoutedEventArgs)
VM.AddDetail()
MyList.ScrollIntoView(MyList.Items(MyList.Items.Count - 1))
Dim ListBoxItem As ListBoxItem = MyList.ItemContainerGenerator.ContainerFromItem(MyList.SelectedItem)
ListBoxItem.Focus()
End Sub
在我的孩子UserControl
身上,我在 XAML 中使用了这个:
FocusManager.FocusedElement="{Binding ElementName=txtMyBox}"
解决方案
这里有一个相关的问题,大多数方法都使用挂钩焦点事件来实现焦点变化。我想提出另一种基于遍历可视化树的解决方案。不幸的是,我只能为您提供 C# 代码,但您可以使用该概念将其应用到您的 Visual Basic 代码中。
据我所知,您正在使用代码隐藏将您的项目滚动到视图中。我会以此为基础。您的列表框有列表框项,我猜您使用数据模板将UserControl
s 显示为子项。在这些用户控件中,您已通过属性在 XAML 中或在代码隐藏中TextBox
分配了一个名称。现在,您需要一个辅助方法来遍历可视化树并搜索文本框。x:Name
Name
private IEnumerable<TextBox> FindTextBox(DependencyObject dependencyObject)
{
// No other child controls, break
if (dependencyObject == null)
yield break;
// Search children of the current control
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
{
var child = VisualTreeHelper.GetChild(dependencyObject, i);
// Check if the current item is a text box
if (child is TextBox textBox)
yield return textBox;
// If we did not find a text box, search the children of this child recursively
foreach (var childOfChild in FindTextBox(child))
yield return childOfChild;
}
}
然后我们添加一个使用 Linq 过滤给定名称的可枚举文本框的方法。
private TextBox FindTextBox(DependencyObject dependencyObject, string name)
{
// Filter the list of text boxes for the right one with the specified name
return FindTextBox(dependencyObject).SingleOrDefault(child => child.Name.Equals(name));
}
在您的bnAdd
处理程序中,您可以使用ListBoxItem
, 搜索文本框子项并将其聚焦。
var textBox = FindTextBox(listBoxItem, "MyTextBox");
textBox.Focus();
推荐阅读
- javascript - Suitescript:无法从搜索中获取结果
- python - Python:这个数组元素替换正确吗?
- node.js - 在 find() mongoDB/mongoose 查询结果中格式化日期?
- angular - Angular 8 ngx-cookie 2.4 无法从 FireFox 读取(即使它存在)但适用于 Chrome/Edge
- sql - 寻找一种在 SQL 中过滤这些 UNION 查询结果的方法
- ruby-on-rails - 使用 Faker 时 Rspec 'trait 未注册错误
- c++ - winapi 中的 MailSlot 问题,ReadFile 不从 mailslot 读取值
- sql - 来自另一个 INNER JOIN 的 INNER JOIN
- android - 我无法将数据发送到 sqlite 到数据库
- r - httr:curl::curl_fetch_memory(url,handle = handle)中的错误:从对等方接收数据时失败 - FileSize?