2017-08-30 89 views
0

我們在我們的項目中使用Mindfusion wpf鍵盤。Wpf虛擬鍵盤對話框竊取文本框鍵盤焦點

每當文本框聚焦時,我們都會打開用戶的鍵盤對話框。

如果沒有任何行爲的常規文本框一切正常,但是當我們使用此鍵盤作爲「全選」行爲的文本框時,我們只能輸入一個字符,因爲它在鍵盤上的按鍵之後選擇了文本。

我們檢查了它,這與mindfusion鍵盤沒有問題,因爲當我們將它用作用戶控件時,它可以工作。當我們從他們的應用程序打開這個鍵盤時,它就可以工作當我們打開Windows鍵盤時,它就可以工作。

我認爲這與對話框窗口大部分是一樣的。

我們試圖設置它爲focusable=falseshowactivated=false,它不工作。

我們還試圖用focusmanger和win32.showunactivated

這裏是行爲的代碼:

public class SelectAllTextOnFocusBehavior : Behavior<TextBox> 
{ 
    protected override void OnAttached() 
    { 
     base.OnAttached(); 
     AssociatedObject.GotKeyboardFocus += AssociatedObjectGotKeyboardFocus; 
     AssociatedObject.GotMouseCapture += AssociatedObjectGotMouseCapture; 
     AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObjectPreviewMouseLeftButtonDown; 
    } 

    protected override void OnDetaching() 
    { 
     base.OnDetaching(); 
     AssociatedObject.GotKeyboardFocus -= AssociatedObjectGotKeyboardFocus; 
     AssociatedObject.GotMouseCapture -= AssociatedObjectGotMouseCapture; 
     AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObjectPreviewMouseLeftButtonDown; 
    } 

    private void AssociatedObjectGotKeyboardFocus(object sender, 
     System.Windows.Input.KeyboardFocusChangedEventArgs e) 
    { 
     AssociatedObject.SelectAll(); 
     Console.WriteLine("AssociatedObjectGotKeyboardFocus"); 
    } 

    private void AssociatedObjectGotMouseCapture(object sender, 
     System.Windows.Input.MouseEventArgs e) 
    { 
     AssociatedObject.SelectAll(); 
     Console.WriteLine("AssociatedObjectGotMouseCapture"); 
    } 

    private void AssociatedObjectPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     if (!AssociatedObject.IsKeyboardFocusWithin) 
     { 
      AssociatedObject.Focus(); 
      Console.WriteLine("AssociatedObjectPreviewMouseLeftButtonDown"); 

      e.Handled = true; 
     } 
    } 
} 

你有任何想法如何防止它丟失鍵盤焦點?

回答

0

的解決方案是在另一個線程要打開的窗口...... 我們爲了解決這個問題浪費12小時..

 var thread = new Thread(() => 
     { 
      fk = new FullKeyboard(); 
      fk.Show(); 
      App.Current.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted; 
      System.Windows.Threading.Dispatcher.Run(); 
     }); 

     thread.SetApartmentState(ApartmentState.STA); 
     thread.Start();