消除Combobox显示下拉框后自动选中文本效果

public partial class MyCombobox : ComboBox
{
    public MyCombobox()
    {
        InitializeComponent();
    }

    private int caretPosition;

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var element = GetTemplateChild("PART_EditableTextBox");
        if (element != null)
        {
            var textBox = (TextBox)element;
            textBox.SelectionChanged += OnDropSelectionChanged;
        }
    }

    private void OnDropSelectionChanged(object sender, System.Windows.RoutedEventArgs e)
    {
        TextBox txt = (TextBox)sender;

        if (base.IsDropDownOpen && txt.SelectionLength > 0)
        {
            txt.CaretIndex = caretPosition;
        }
        if (txt.SelectionLength == 0 && txt.CaretIndex != 0)
        {
            caretPosition = txt.CaretIndex;
        }
    }
}
把圈子变小,把语言变干净,把成绩往上提,把故事往心里收,现在想要的以后你都会有。
原文地址:https://www.cnblogs.com/jizhiqiliao/p/15602666.html