控制不集中

时间:2014-01-20 11:19:49

标签: wpf wpf-controls wpfdatagrid

我有一个带有多个子控件的WPF用户控件,我使用以下代码以编程方式聚焦DataGrid和TextBox:

searchTextBox.Focus();

productGrid.Focus();

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

searchTextBox正常聚焦但dataGrid不聚焦(键盘焦点停留在某些其他控件上)。下面我提供了隐藏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。

1 个答案:

答案 0 :(得分:0)

似乎productGrid。Focusable属性设置为"false"。您应该将其设置为"True",并且您的代码应该正常运行,并且无需调用Keyboard.Focus()方法。