我有一个 ComboBox 声明如下:
<ComboBox Name="txtUserName" IsEditable="True" />
我想选中 ComboBox 的文本字段,但我不知道该怎么做。目前,当 ComboBox 以编程方式(通过“txtUserName.Focus()”)获得焦点时,它允许用户滚动浏览不同的项目,但需要额外单击以突出显示文本字段。
有什么想法吗?
最佳答案
我使用的解决方案是在窗口的加载事件中添加以下代码:
var textBox = (txtUserName.Template.FindName("PART_EditableTextBox", txtUserName) as TextBox);
if (textBox != null)
{
textBox.Focus();
textBox.SelectionStart = textBox.Text.Length;
}
解决方案来自此处的建议答案之一:How to add a focus to an editable ComboBox in WPF
关于c# - 选择组合框文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19728650/