简体   繁体   English

KeyBoard focus wpf:从其他UserControls TreeView按TAB时,将KeyBoard Focus设置为DataGrids第一行

[英]KeyBoard focus wpf : Set KeyBoard Focus to a DataGrids First row when Pressed TAB from a different UserControls TreeView

I have two UserControls One has a TreeView and another has a Button and a DataGrid . 我有两个UserControls一个具有TreeView ,另一个具有ButtonDataGrid

What i am trying to achieve is when Tab on a TreeViewItem should give KeyBoard Focus to the DataGrid in second UserControl. 我想要实现的是,当TreeViewItem上的Tab应将KeyBoard Focus赋予第二个UserControl中的DataGrid时。

I have looked in to different posts but no luck. 我看过不同的职位,但没有运气。 find my XAML below, 在下面找到我的XAML,

<Grid>
  <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />            
    </Grid.RowDefinitions>
   <UC:ExplorerView DataContext="{Binding ExplorerViewModel}" Grid.Row="0"/>
   <UCs:TableOfContentView DataContext="{Binding TableOfContentViewModel}" x:Name="TOCView" Grid.Row="1"/>
</Grid>

simplified XAML for the question. 该问题的简化XAML。

I have tried to set the focus to the Second UserControl by adding event PreviewKeyDown 我试图通过添加事件PreviewKeyDown将焦点设置为Second UserControl

 private void ExplorerView_KeyDown(object sender, KeyEventArgs e)
    {
        if(e.Key == Key.Tab)
        {                
            Keyboard.Focus(TOCView);
        }
    }

But this give focus to the USerControl not to the DataGrid as i mentioned above. 但这将重点放在USerControl而不是我上面提到的DataGrid上。

Tried to Attach a Property to the DataGrid . 试图Attach Property AttachDataGrid it worked as expected but not giving focus to the first row . 它按预期进行了,但没有把重点放在first row this thread given the input for that. 该线程为此提供了输入。


Solved :) 解决了 :)

Created the AttachedProperty mentioned in the thread above and modified the callback method to set the focus to the DataGrids First Row like below, 创建了上面线程中提到的AttachedProperty ,并修改了回调方法以将焦点设置为DataGrids First Row,如下所示,

  private static object OnCoerceValue(DependencyObject d, object baseValue)
    {  
        if (((DataGrid)d).HasItems)
        {
            DataGridRow row = ((DataGrid)d).ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow;
            if(row !=null)
            { 
                if ((bool)baseValue)
                {
                    FocusManager.SetIsFocusScope(row, true);
                    FocusManager.SetFocusedElement(row, row);
                }
                else if (((UIElement)d).IsFocused)
                    Keyboard.ClearFocus();
            }
        }
        return ((bool)baseValue);            
    }

Please feel free to add any better solution. 请随时添加任何更好的解决方案。 Thanks in Advance. 提前致谢。

Created the AttachedProperty mentioned in the thread above and modified the callback method to set the focus to the DataGrids First Row like below, 创建了上面线程中提到的AttachedProperty ,并修改了回调方法以将焦点设置为DataGrids First Row,如下所示,

  private static object OnCoerceValue(DependencyObject d, object baseValue)
    {  
        if (((DataGrid)d).HasItems)
        {
            DataGridRow row = ((DataGrid)d).ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow;
            if(row !=null)
            { 
                if ((bool)baseValue)
                {
                    FocusManager.SetIsFocusScope(row, true);
                    FocusManager.SetFocusedElement(row, row);
                }
                else if (((UIElement)d).IsFocused)
                    Keyboard.ClearFocus();
            }
        }
        return ((bool)baseValue);            
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM