2012-09-07 2 views
1

"SelectAllOnFocus"라는 첨부 된 속성이 있습니다. 참/거짓 값.TextBox AttachedProperty를 선택하여 모든 텍스트가 예상대로 작동하지 않습니까?

  1. PreviewMouseDown 이벤트가 트리거됩니다 : 어떻게됩니까

    public static class TextBoxProps 
        { 
         private static void MyTextBoxKeyUp(object sender, KeyEventArgs e) 
         { 
          if (e.Key == Key.Escape) 
          { 
           ((TextBox)sender).Text = string.Empty; 
          } 
         } 
    
         public static void SetSelectAllOnFocus(DependencyObject dependencyObject, bool  selectAllOnFocus) 
         { 
          if (!ReferenceEquals(null, dependencyObject)) 
          { 
           dependencyObject.SetValue(SelectAllOnFocus, selectAllOnFocus); 
          } 
        } 
    
        public static bool GetSelectAllOnFocus(DependencyObject dependencyObject) 
        { 
         if (!ReferenceEquals(null, dependencyObject)) 
         { 
          return (bool)dependencyObject.GetValue(SelectAllOnFocus); 
         } 
         else 
         { 
          return false; 
         } 
        } 
    
        private static void OnSelectAllOnFocus(DependencyObject d, DependencyPropertyChangedEventArgs e) 
        { 
         bool selectAllOnFocus = (bool)e.NewValue == true; 
         var theTextBox = d as TextBox; 
    
         if (selectAllOnFocus && theTextBox != null) 
         { 
          theTextBox.PreviewMouseDown -= MyTextBoxMouseEnter; theTextBox.PreviewMouseDown += MyTextBoxMouseEnter; 
         } 
        } 
    
        private static void MyTextBoxMouseEnter(object sender, MouseEventArgs e) 
        { 
         ((TextBox)sender).SelectAll(); 
         e.Handled = false; 
        } 
    
    
        public static readonly DependencyProperty SelectAllOnFocus 
         = DependencyProperty.RegisterAttached("SelectAllOnFocus", typeof(bool), typeof(TextBoxEscapeProperty), 
          new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnSelectAllOnFocus))); 
    } 
    

    는 다음과 같습니다.

  2. MyTextBoxMouseEnter 메서드가 호출됩니다.
  3. SelectAll() 메서드가 호출됩니다.
  4. ((TextBox) 보낸 사람) .SelectedText에서 "watch"를 수행하면 값이 정확합니다 (텍스트 상자에있는 내용이 selectedText로 표시됨).
  5. 텍스트 상자 자체는 변경되지 않습니다. 텍스트가 선택되지 않았습니다.

이것은 일반적인 WPF 스타일의 일부입니다. 응용 프로그램의 모든 텍스트 상자는이 속성과 관련 동작을 수신해야합니다.

내가 곤혹 스럽다. 어떤 아이디어?

감사

당신이 ((텍스트 상자) 보낸 사람) .UpdateLayout()를 호출하면 어떻게됩니까
+0

에서

(당신은 당신의 "SelectAllOnFocus"속성을 확인하기 위해 그것을 수정해야합니다). PreviewMouseDown 대신 GotKeyboardFocus 이벤트를 처리합니다. 그것은 내가 볼 수있는 유일한 중요한 차이입니다. 그 이벤트를 사용해 보셨습니까? –

+0

응답 주셔서 감사합니다 토마스 ... 난 그냥 PreviewMouseButtonDown 대신 PreviewMouseLeftButtonUp를 사용하도록 전환하고 작동합니다. 그게 왜 어떤 아이디어일까요? – tronious

+0

토마스가이 질문에 대한 답변으로 게시하여 수표를 줄 수 있습니까? PreviewMouseDown이 작동하지 않는 이유는 정확히 모르겠지만 응답은 유효한 답변 – tronious

답변

0

; SelectAll 명령 바로 다음에? 또는 키보드을 텍스트 상자에 포커스를 설정해야 할 수도 있습니다.

텍스트 상자를 마우스 또는 키보드로 선택하면이 기능을 사용하는 것이 더 좋은 옵션 일 수 있습니다. 내가 잘 작동 매우 유사한 구현이 당신의 App.xaml.cs를

protected override void OnStartup(StartupEventArgs e) 
    { 
     // Select the text in a TextBox when it receives focus. 
     EventManager.RegisterClassHandler(typeof(TextBox), TextBox.PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(SelectivelyIgnoreMouseButton)); 
     EventManager.RegisterClassHandler(typeof(TextBox), TextBox.GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText)); 
     EventManager.RegisterClassHandler(typeof(TextBox), TextBox.MouseDoubleClickEvent, new RoutedEventHandler(SelectAllText)); 
     base.OnStartup(e); 
    } 

    void SelectivelyIgnoreMouseButton(object sender, MouseButtonEventArgs e) 
    { 
     // Find the TextBox 
     DependencyObject parent = e.OriginalSource as UIElement; 
     while (parent != null && !(parent is TextBox)) 
      parent = VisualTreeHelper.GetParent(parent); 

     if (parent != null) 
     { 
      var textBox = (TextBox)parent; 
      if (!textBox.IsKeyboardFocusWithin) 
      { 
       // If the text box is not yet focused, give it the focus and 
       // stop further processing of this click event. 
       textBox.Focus(); 
       e.Handled = true; 
      } 
     } 
    } 

    void SelectAllText(object sender, RoutedEventArgs e) 
    { 
     var textBox = e.OriginalSource as TextBox; 
     if (textBox != null) 
      textBox.SelectAll(); 
    } 
+0

@user1631520입니다. 왜 PreviewMouseLeftButtonUp이 작동하지만 PreviewMouseDown은 작동하지 않는지 질문했습니다. 나는 그것이 ** 키보드 ** 포커스가 아직 PreviewMouseDown의 텍스트 상자로 설정되지 않았지만 PreviewMouseLeftButtonUp에 이미 설정되어 있다고 의심합니다. – StillLearnin

관련 문제