0

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.

Sivasubramanian
  • 935
  • 7
  • 22
  • BTW, this is completely wrong way to do things in WPF. You should be using [Binding Validation](http://msdn.microsoft.com/en-us/library/ms753962(v=vs.110).aspx). – Aron Apr 28 '14 at 08:37
  • Actually see this instead http://stackoverflow.com/questions/11223236/binding-to-double-field-with-validation – Aron Apr 28 '14 at 08:39
  • Hi Aron, Thanks, I tried using Validation Rule. But I found another way which is more feasible for my project. – Sivasubramanian Apr 28 '14 at 11:11

2 Answers2

1

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
        }
    }
}
Sheridan
  • 68,826
  • 24
  • 143
  • 183
0

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.

akshay2000
  • 823
  • 8
  • 28
  • Hi Akshay, In my comboBox user can directly enter the text. I have set ComboBox.IsEditable is set to true. By that case your solution will not work. – Sivasubramanian Apr 28 '14 at 08:12
  • Right, it wouldn't. But why do you want it to be editable? If you want to let people add new items, just have another TextBox somewhere and add entries yourself to the list to which combobox is bound. – akshay2000 Apr 28 '14 at 08:16
  • Hi Akshay, Thanks for your try. that solution will not be feasible solution to my project. Thrid party controls like DevExpress allows users to enter only numeric values in their comboBox. I am trying to implement the same feature in WPF comboBox. – Sivasubramanian Apr 28 '14 at 08:22