C# XAML自定义文本框事件和属性未实时更新

C# XAML自定义文本框事件和属性未实时更新,c#,xaml,win-universal-app,uwp,uwp-xaml,C#,Xaml,Win Universal App,Uwp,Uwp Xaml,从TextBox继承并连接TextChanged事件后,订阅TextChanged事件时,Text属性不准确 这是我的控制: [TemplatePart(Name = "PART_ctlTextBox", Type = typeof(TextBox))] public class CustomTextBox : Control { public CustomTextBox() { DefaultStyleKey = typeof(CustomTextBox);

TextBox
继承并连接TextChanged事件后,订阅TextChanged事件时,Text属性不准确

这是我的控制:

[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));
}
以下是通用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>

请注意,常规的
TextBox
会实时更新,而
CustomTextBox
会延迟。我也尝试过用
操作点击TextChanged。调用
没有效果。

你所说的“常规文本框实时更新,而CustomTextBox延迟”是什么意思?我看不出有什么不同。与常规文本框相比,
CustomTextBox
的文本块标签更新似乎延迟了1个字符。例如,如果我在两个文本框中键入“123”,则
lblRegularTextBox
将显示“123”,但
lblCustomTextBox
将仅显示“12”。您所说的“常规文本框实时更新,而自定义文本框延迟”是什么意思?我看不出有什么不同。与常规文本框相比,
CustomTextBox
的文本块标签更新似乎延迟了1个字符。例如,如果我在两个文本框中都键入“123”,则
lblRegularTextBox
将显示“123”,但
lblCustomTextBox
将仅显示“12”
    <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>
    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;
    }