我正在尝试创建一个适用于Windows Phone 8(Silverlight)的文本框,其行为类似于普通文本框,但具有特殊行为。
这个特殊行为是当用户输入时,即时更新ViewModel,而不是等到文本框失去焦点时才更新。
这是我现在拥有并且正在运作的。
<TextBox Text="{Binding Path=Email,Mode=TwoWay}" InputScope="EmailNameOrAddress">
<i:Interaction.Behaviors>
<helpers:UpdateTextBindingOnPropertyChanged />
</i:Interaction.Behaviors>
</TextBox>
我希望创建一个FastTextbox子类,该子类的默认行为是文本框中的内容快速输入。
我如何通过编程方式添加此行为?
我尝试了以下代码:
public class FastTextbox:System.Windows.Controls.TextBox
{
public FastTextbox()
{
BehaviorCollection Behaviors= Interaction.GetBehaviors(this);
Behaviors.Add(new UpdateTextBindingOnPropertyChanged());
}
}
但是我的行为出现了错误。
我使用的行为使用以下代码来确定其表达式(失败了)。
protected override void OnAttached()
{
base.OnAttached();
// expression gets null here :(
_expression = AssociatedObject.GetBindingExpression(TextBox.TextProperty);
AssociatedObject.TextChanged += OnTextChanged;
}
我该怎么做呢?
InitializeComponent
仅适用于用户控件。 - Martin