0

这是我的自定义控件,具有自定义 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 ""!
}
4

1 回答 1

1

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

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

于 2013-03-28T21:26:59.570 回答