XAML Custom Textbox Events and Properties not Updating in Real Time

本小妞迷上赌 提交于 2019-12-11 07:31:37

问题


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.

来源:https://stackoverflow.com/questions/39192467/xaml-custom-textbox-events-and-properties-not-updating-in-real-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!