如何在WPF中的可编辑ComboBox中获取文本 [英] How to got the text in the editable ComboBox in WPF

查看:928
本文介绍了如何在WPF中的可编辑ComboBox中获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可编辑的ComboBox,其模板如下。我正在尝试访问文本框"PART_EditableTextBox"在下面的组合框里面。但它总是返回null。

I have a editable ComboBox with template like below. i am trying the access the textbox "PART_EditableTextBox" inside the combobox like below. But it always returns null.

<ControlTemplate TargetType="{x:Type ComboBox}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="50"/>
                        </Grid.ColumnDefinitions>
                        <TextBox Name="PART_EditableTextBox"
                                     Style="{StaticResource ComboBoxTextBoxStyle}"
                                     Height="{TemplateBinding Height}"/>





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

            _textBox = Template.FindName("PART_EditableTextBox", this) as TextBox;
            if (_textBox != null)
            {
                _textBox.KeyUp += _handleTextChanged;
                _textBox.LostFocus += _lostTextFocus;
            }
        }

推荐答案

很抱歉我迟到的回复。

你确定TextBox 是ComboBox的一部分吗?模板?

Would you sure the TextBox as the part of ComboBox in Template?

在默认模板中,您需要将  IsEditable属性设置为True,然后ComboBox将在触发器中使用ComboBoxEditableTemplate。

In the default Template, you need set  IsEditable property to True, then ComboBox would use the ComboBoxEditableTemplate in trigger.

可能是因为某些原因造成的延迟,此控件的模板尚未应用,因此FindName不会返回任何有用的内容。

Maybe because of delays caused by some reason, this control's template has not been applied, so FindName does not return anything useful.

您可以尝试c <\\ n> span>所有ApplyTemplate()在 模板 FindName()之前。
应用程序可以调用此方法来保证元素的可视树完整。

You can try to call ApplyTemplate() before Template.FindName(). Applications can call this method to guarantee that the visual tree of an element is complete.

https://stackoverflow.com/questions/23891424/gettemplatechild-returns-null-when-dependency-property-has-relativesource-绑定

最好的问候,

鲍勃


这篇关于如何在WPF中的可编辑ComboBox中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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