moswald
moswald

Reputation: 11667

I have a custom control based on TextBox, but the Text doesn't change in the TextChanged handler

Here's my custom control, with the custom xaml style:

using System.Windows.Controls;

namespace MyControls
{
    public class CustomTextBox : TextBox
    {
    }
}

<Style TargetType="controls:CustomTextBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="controls:CustomTextBox">
                <StackPanel>
                    <TextBlock Text="" />
                    <TextBox Text="{TemplateBinding Text}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And when used, the TextChanged handler doesn't get an updated Text field for some reason.

<controls:CustomTextBox x:Name="ControlInstance"
                        TextChanged="OnTextChanged"
                        InputScope="EmailNameOrAddress" />

private void OnTextChanged(object sender, TextChangedEventArgs e)
{
    // ControlInstance.Text is always ""!
}

Upvotes: 0

Views: 799

Answers (1)

Paul Annetts
Paul Annetts

Reputation: 9604

That's because you're changing the Text property of the inner TextBox in your template, but not the Text property of your CustomTextBox itself.

I recommend you check out http://www.geekchamp.com/articles/creating-a-wp7-custom-control-in-7-steps for help in creating your custom control.

Upvotes: 1

Related Questions