1

I have two UserControls One has a TreeView and another has a Button and a DataGrid.

What i am trying to achieve is when Tab on a TreeViewItem should give KeyBoard Focus to the DataGrid in second UserControl.

I have looked in to different posts but no luck. find my XAML below,

<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.

I have tried to set the focus to the Second UserControl by adding event PreviewKeyDown

 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.

Tried to Attach a Property to the DataGrid. it worked as expected but not giving focus to the 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,

  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.

Community
  • 1
  • 1
Abin
  • 2,868
  • 1
  • 23
  • 59
  • "I have looked in different posts"? Well, what did you try that didn't work for you? Instead of just stating what your expected outcome is, please provide your actual outcome as well. – Patrick Sep 28 '15 at 20:01
  • @Patrick Added some input to the question about what i have tried. – Abin Sep 28 '15 at 20:08
  • Is it an option to modify the UserControl(s)? – Yacoub Massad Sep 28 '15 at 20:33
  • @YacoubMassad Yes sure i can modify it. – Abin Sep 28 '15 at 20:36
  • Currently, if you press tab from a `TreeViewItem` what happens? does it give focus to the button? Why not simply set the `TabIndex` property on the `DataGrid` to 1 and on the `Button` to 2? – Yacoub Massad Sep 28 '15 at 20:45
  • @YacoubMassad I have now an `AttachedProperty`. which will set the focus to DataGrid but i need it to be `focused` to the `first item` in the `DataGrid`. `AttachedProperty` thread added to the question. – Abin Sep 28 '15 at 20:53

1 Answers1

3

Created the AttachedProperty mentioned in the thread above and modified the callback method to set the focus to the DataGrids First Row like below,

  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);            
    }
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Abin
  • 2,868
  • 1
  • 23
  • 59