简体   繁体   中英

How to register the TextChanged event for the TextBox which is inside ComboBox

We know that comboBox is a combination of TextBox, Button and other UIElements. My question is How to register the TextChanged event for the TextBox which is inside ComboBox. ComboBox contains only PreviewTextInput and TextInput events but I want to handle TextChangedEvent. Edit:1 My comboBox is an Editable Combobox

Edit:2 When the user enters text in ComboBox, I want to check whether it is a double value or not. I allow only double values in my comboBox.

Thanks in Advance.

If you extend the ComboBox class, you can override the OnPropertyChanged method. This method will be called each time any property of the ComboBox is changed, including the Text property. Try this:

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

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);
        if (e.Property.Name.Contains("Text")) 
        {
            // The Text property value has changed
        }
    }
}

You could use a custom template for ComboBox items like this:

<ComboBox>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <!--Your items with whatever-->
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

However, SelectionChanged event should tell you when the selection is changed and hence, the text is changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM