XAML Custom Textbox Events and Properties not Upda

2019-07-27 03:51发布

问题:

After a creating and changing my custom Win 8.1/UWP TextBox control to inherit from TextBox and wiring up the TextChanged event, the Text property is not accurate when subscribing to the TextChanged event.

Here's my control:

[TemplatePart(Name = "PART_ctlTextBox", Type = typeof(TextBox))]
public class CustomTextBox : Control
{
    public CustomTextBox()
    {
        DefaultStyleKey = typeof(CustomTextBox);
    }

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

        var textBox = GetTemplateChild("PART_ctlTextBox") as TextBox;
        if (textBox != null)
        {
            textBox.TextChanged += (s, a) => TextChanged?.Invoke(s, a);
        }
    }

    public event TextChangedEventHandler TextChanged;

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(CustomTextBox), new PropertyMetadata(null));
}

Here's the generic XAML:

<ResourceDictionary 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:customControls="using:CustomControls">
    <Style TargetType="customControls:CustomTextBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="customControls:CustomTextBox">
                    <TextBox x:Name="PART_ctlTextBox" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Path=Text, UpdateSourceTrigger=PropertyChanged}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Here's a page I'm using to test it:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock x:Name="lblCustomTextBox" Grid.Row="0" VerticalAlignment="Bottom" />
        <local:CustomTextBox Grid.Row="1" x:Name="txtCustomTextBox" TextChanged="customTextBox_OnTextChanged" />
        <TextBlock x:Name="lblRegularTextBox" Grid.Row="2" VerticalAlignment="Bottom" />
        <TextBox x:Name="txtRegularTextBox" Grid.Row="3" TextChanged="regularTextBox_OnTextChanged"></TextBox>
    </Grid>

And the code-behind:

    private void customTextBox_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        lblCustomTextBox.Text = txtCustomTextBox.Text ?? string.Empty;
    }

    private void regularTextBox_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        lblRegularTextBox.Text = txtRegularTextBox.Text ?? string.Empty;
    }

Notice the regular TextBox gets updated in real time whereas the CustomTextBox is delayed. I've also tried hitting the TextChanged with an Action.Invoke with no avail.