我已经搜索了类似的问题,但找不到任何内容。 .Caret 似乎不可用,我不知道如何深入到文本框或组合框中嵌入的任何控件。
最佳答案
您需要从组合框的控件模板中获取 PART_EditableTextBox
控件。最简单的方法是覆盖 OnApplyTemplate
在 ComboBox
的推导中然后在任何需要具有这种扩展行为的组合框的地方使用该推导。
protected void override OnApplyTemplate()
{
var myTextBox = GetTemplateChild("PART_EditableTextBox") as TextBox;
if (myTextBox != null)
{
this.editableTextBox = myTextBox;
}
}
一旦有了文本框,就可以设置插入符位置,设置SelectionStart
到您希望插入符号出现的位置并设置 SelectionLength
归零。
public void SetCaret(int position)
{
this.editableTextBox.SelectionStart = position;
this.editableTextBox.SelectionLength = 0;
}
关于c# - 如何在 WPF 文本可编辑组合框中设置插入符位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5067931/