由于WPF Combobox在C#中没有像Textbox和SelectionStart属性那样的CaretIndex属性,我想知道如何获得我的WPF Combobox控件的插入索引?
谢谢你们。
答案 0 :(得分:4)
它不能直接访问,但你可以从它的子控件中获取它。您需要从组合框的控件模板获取PART_EditableTextBox控件。最简单的方法是在ComboBox的派生中重写OnApplyTemplate,然后使用该派生来订阅更改的选择。然后你可以得到CaretIndex
protected void override OnApplyTemplate()
{
var myTextBox = GetTemplateChild("PART_EditableTextBox") as TextBox;
if (myTextBox != null)
{
myTextBox .SelectionChanged += OnDropSelectionChanged;
}
}
private void OnDropSelectionChanged(object sender, RoutedEventArgs e)
{
var textbox = sender as TextBox;
if (textbox != null)
{
//you can write your own logic.
_carentIndex = textbox.CaretIndex;
}
}
确保您在配置或适当的地方取消订阅SelectionChanged
myTextBox .SelectionChanged -= OnDropSelectionChanged;