wpf - 在代码隐藏中聚焦最后一个 ListBoxItem
问题描述
我正在向我添加一个新项目,ObservableCollection
用作listBox.ItemsSource
:
ObservableCollection<MySrc> mySrc;
// in window constructor
mySrc = App.Database.GetSrc();
listBox.ItemsSource = mySrc;
单击按钮时,我想将新项目添加到 ObservableCollection 并关注新添加的项目。我的控件模板包含TextBox
元素。我希望 TextBox 获得一个光标(如果它默认获得焦点,就会发生这种情况)
private void AddButton_Click(object sender, RoutedEventArgs e) {
MySrcnewMatch = MySrc.PopulateMySrc("Default name", true, true);
this.mySrc.Add(mySrc);
listBox.SelectedIndex = listBox.Items.Count-1;
((ListBoxItem)listBox.SelectedItem).Focus();
}
不幸的是,我收到一个错误:
Unable to cast object of type 'Namespace.Model.MySrc' to type 'System.Windows.Controls.ListBoxItem'.
ListBoxItem
将最后一个焦点集中在的正确方法是什么ListBox
解决方案
推荐阅读
- django - Django:如何覆盖默认错误消息?
- ionic3 - 我在传递 URL 时在 ionic InAppBrowser 中遇到错误
- stripe-payments - 如何在代表关联账户发送的发票上收取申请费
- angular - 以角度反应形式验证重复项
- datastax-enterprise - 无法在 DSE 中启用 Alwayson sql
- python - 根据其他列表值查找替换列表值
- neo4j - 你如何在 Neo4j 上显示一些节点的字段?
- node.js - 安装 napi-js 或 @adonis/cli 时出现 NPM 错误
- android - android alertdialog 主题不一致错误
- html - 半径圆菜单