我有一个基于TextBox的自定义控件,但Text没有';t在TextChanged处理程序中发生更改

本文关键字:TextChanged 处理 程序 TextBox 有一个 自定义控件 没有 Text | 更新日期: 2023-09-27 18:06:28

这是我的自定义控件,具有自定义xaml样式:

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>

当使用时,由于某种原因,TextChanged处理程序不会获得更新的Text字段。

<controls:CustomTextBox x:Name="ControlInstance"
                        TextChanged="OnTextChanged"
                        InputScope="EmailNameOrAddress" />
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
    // ControlInstance.Text is always ""!
}

我有一个基于TextBox的自定义控件,但Text没有';t在TextChanged处理程序中发生更改

这是因为您正在更改模板中内部TextBox的Text属性,而不是CustomTextBox本身的Text属性。

我建议你退房http://www.geekchamp.com/articles/creating-a-wp7-custom-control-in-7-steps以获取创建自定义控件的帮助。