2014-01-20 29 views
2

我曾與多個子控件WPF用戶控件,我編程聚焦的DataGrid和TextBox用下面的代碼:控制不注重

searchTextBox.Focus(); 

productGrid.Focus(); 

productGrid.Focus(dataGrid); //tried this but it does not help 

searchTextBox正常,但的數據網格的重點不(鍵盤焦點停留在其他控件上)。下面我提供隱藏searchTextBox和焦點移動到productGrid完整的源代碼(searchPanel是searchTextBox的父網格):

private void Execute_CancelCommand(object sender, ExecutedRoutedEventArgs e) 
    { 
     if (searchPanel.Visibility == Visibility.Visible) 
     { 
      searchPanel.Visibility = Visibility.Collapsed; 

      searchTextBox.Clear(); 

      searchTextBox.Background = Brushes.White; 

      //the focus stays on the splitter for some reason 
      productGrid.Focus(); 

      Keyboard.Focus(productGrid); 
     } 
    } 

是什麼導致這種情況呢?

Thnx。

回答

0

看來是productGrid。 Focusable屬性設置爲"false"。您應該將其設置爲"True",並且您的代碼應該正常工作,並且不需要調用Keyboard.Focus()方法。

+1

請添加評論*作爲評論*和*不*作爲答案。 [理想情況下,應該包含多於兩個句子](http://stackoverflow.com/help/how-to-answer)。 – Sheridan

+0

productGrid.Focusable設置爲true(我使用調試器進行了檢查),但看起來像productGrid部分聚焦,因爲它突出顯示了具有藍色矩形的選定行,但虛線矩形指示鍵盤焦點停留在某個其他控件上。請參閱http://slogpost.ru/wp-content/uploads/2014/01/search-off.png,完全不重點的網格如下所示:http://slogpost.ru/wp-content/uploads/2014/01/ search-on.png – Dmitriano

+0

我對標準網格不太確定,但它可能會保持專注於自身並處理鍵盤事件以在行之間導航等。因此,如果它只是一個視覺問題而不是行爲,我建議您設置DataGrid.FocusVisualStyle屬性在代碼中爲null(或者在XAML中爲{x:Null})。 2Sankarann:對不起,我確定我的想法是解決這個問題的答案。 – Alexis