【发布时间】:2017-03-22 12:41:24
【问题描述】:
TextBox 上的双向 Binding 有问题。
<TextBox Text="{Binding Path=MyText, Mode="TwoWay", UpdateSourceTrigger=LostFocus}" />
在离开这个元素的焦点时,我希望有一个 MyText 的 setter 调用,即使 Text 属性没有改变。
public string MyText {
get { return _myText; }
set {
if (value == _myText) {
RefreshOnValueNotChanged();
return;
}
_myText = value;
NotifyOfPropertyChange(() => MyText);
}
}
永远不会调用测试函数RefreshOnValueNotChanged()。有谁知道诀窍吗?我需要UpdateSourceTrigger=LostFocus,因为Enter 的附加行为(并且我需要完整的用户输入...)。
<TextBox Text="{Binding Path=MyText, Mode="TwoWay", UpdateSourceTrigger=LostFocus}" >
<i:Interaction.Behaviors>
<services2:TextBoxEnterBehaviour />
</i:Interaction.Behaviors>
</TextBox>
类:
public class TextBoxEnterBehaviour : Behavior<TextBox>
{
#region Private Methods
protected override void OnAttached()
{
if (AssociatedObject != null) {
base.OnAttached();
AssociatedObject.PreviewKeyUp += AssociatedObject_PKeyUp;
}
}
protected override void OnDetaching()
{
if (AssociatedObject != null) {
AssociatedObject.PreviewKeyUp -= AssociatedObject_PKeyUp;
base.OnDetaching();
}
}
private void AssociatedObject_PKeyUp(object sender, KeyEventArgs e)
{
if (!(sender is TextBox) || e.Key != Key.Return) return;
e.Handled = true;
((TextBox) sender).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
#endregion
}
【问题讨论】:
标签: wpf data-binding setter updatesourcetrigger