2017-03-22 17 views
0

TextBoxという2つの問題があります。この要素のフォーカスを残すにはLostFocusセッター呼び出しがバインドされていません

<TextBox Text="{Binding Path=MyText, Mode="TwoWay", UpdateSourceTrigger=LostFocus}" /> 

私はTextプロパティが変更されていない場合でも、MyTextのセッターコールを持っていると思います。

public string MyText { 
    get { return _myText; } 
    set { 
     if (value == _myText) { 
      RefreshOnValueNotChanged(); 
      return; 
     } 
     _myText = value; 
     NotifyOfPropertyChange(() => MyText); 
    } 
} 

テスト機能RefreshOnValueNotChanged()は決して呼び出されません。誰かがトリックを知っていますか? Enterの動作が添付されているため、UpdateSourceTrigger=LostFocusが必要です(ユーザー入力が完了する必要があります)。クラスと

<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 
} 

答えて

0

私は自分自身に回避策を見つけました。しかし、誰かがこれより良い解決策を持っているかもしれません。今私はGotFocusの値を操作します。そして、セッターがフォーカスコントロールを離れる際に必ず呼び出された...と

<TextBox Text="{Binding Path=MyText, Mode="TwoWay", UpdateSourceTrigger=LostFocus}" GotFocus="OnGotFocus" > 
    <i:Interaction.Behaviors> 
     <services2:TextBoxEnterBehaviour /> 
    </i:Interaction.Behaviors> 
</TextBox> 

private void OnGotFocus(object sender, RoutedEventArgs e) 
{ 
    var tb = sender as TextBox; 
    if(tb == null) return; 
    var origText = tb.Text; 
    tb.Text += " "; 
    tb.Text = origText; 
} 
関連する問題