在WPF C#中将光标焦点设置为可编辑组合框 [英] Set Cursor Focus into Editable Combobox in WPF C#

查看:63
本文介绍了在WPF C#中将光标焦点设置为可编辑组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中有可编辑的组合框,我想通过C#设置焦点,

I have editable combobox in WPF and I want to set focus from C#,

我正在使用Combobox.Focus(),但它仅显示选择内容,但我要用户可以开始输入的编辑选项.

I am using Combobox.Focus(), but it shows only selection but I want edit option where user can start entering.

更新:找出FIX

我最终在组合框中添加了已加载"事件,并按照以下代码编写代码以使其聚焦,并且效果很好

I ended up adding 'Loaded' Event to the Combobox and Wrote following Code to get focus in and it worked Fine

    private void LocationComboBox_Loaded(object sender, RoutedEventArgs e)
    {
        ComboBox cmBox = (System.Windows.Controls.ComboBox)sender;
        var textBox = (cmBox.Template.FindName("PART_EditableTextBox",  
                       cmBox) as TextBox);
        if (textBox != null)
        {
            textBox.Focus();
            textBox.SelectionStart = textBox.Text.Length;
        }


    }

推荐答案

如果我对您的理解正确,那么您会遇到以下情况:将焦点设置为ComboBox并观察可编辑区域内的选定文本,但您希望它只包含空白内部闪烁的插入符号.如果是这样,您可以按照以下方式进行操作:

If I understand you correctly, you have following situation: you set focus to ComboBox and observe selected text inside editable area, but you want it to be empty with only blinking caret inside. If so, you can do it this way:

ComboBox.Focus();
ComboBox.Text = String.Empty;

这篇关于在WPF C#中将光标焦点设置为可编辑组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆