Frage

I have a custom control that essentially wraps a ComboBox and a TextBox into a custom AutoCompleteTextBox. I am attempting to bubble the TextBox.TextChanged event up to my custom controlAutoCompleteTextBox so I can subscribe to the event as you would with any other event from a WPF by declaring the method to use for the subscriber in the XAML and instantiating that method in the code behind.

How do I make the TextBox.TextChanged event trigger a new event for AutoCompleteTextBox called, say, TextChanged so I can subscribe to AutoCompleteTextBox.TextChanged?

I have found a lot on MSDN about it but am having an issue relating it to my instance. Any help would be appreciated

The following is a stripped down version of the class that should provide all the necessary components related to what the class is the implementation of the TextBox.TextChanged event to trigger from.

   public partial class AutoCompleteTextBox : Canvas
{

    private VisualCollection controls;
    public TextBox textBox;
    private ComboBox comboBox;

    public AutoCompleteTextBox()
    {
        controls = new VisualCollection(this);

        // set up the text box and the combo box
        comboBox = new ComboBox();
        comboBox.IsSynchronizedWithCurrentItem = true;
        comboBox.IsTabStop = true;
        comboBox.TabIndex = 1;

        textBox = new TextBox();
        textBox.TextWrapping = TextWrapping;
        if (MaxLength > 0)
        {
            textBox.MaxLength = MaxLength;
        }
        textBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
        textBox.VerticalContentAlignment = TextWrapping == System.Windows.TextWrapping.NoWrap ? System.Windows.VerticalAlignment.Center : System.Windows.VerticalAlignment.Top;
        textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);
        textBox.TabIndex = 0;

        controls.Add(comboBox);
        controls.Add(textBox);
    }

    public int MaxLength { get; set; }

    private void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
    }
}
War es hilfreich?

Lösung

Add an new event to your control / class:

public event EventHandler<EventArgs> MyTextChanged;

In textBox_TextChanged do this:

if (MyTextChanged != null)
{
    MyTextChanged(this, EventArgs.Empty);
}

This can than be enhanced with your custom event args if you would like, etc.

If just named it MyTextChanged to mark what the new event is. You of course can name it TextChanged.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top