none
How to focus first element .ChildrenOfType after changing selected tab in TabControl RRS feed

  • Question

  • Hello, 

    I have this piece of code:

       private void TabDynamicSelectionChanged(object sender, RadSelectionChangedEventArgs e)
            {
                var obecnyTab = tabControl.SelectedItem as RadTabItem;
                if (obecnyTab != null)
                {
                    var page = obecnyTab.Content as Grid;
                    if (page != null)
                    {
                        var grid = (page).ChildrenOfType<RadGridView>().FirstOrDefault();
                        if (grid != null)
                        {
    
                            grid.Focusable = true;
                            grid.MouseDown+= GridOnMouseDoubleClick;
    
                            MouseButtonEventArgs args = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left);
                            args.RoutedEvent = Button.PreviewMouseLeftButtonDownEvent;
                            grid.RaiseEvent(args);
                            //GridOnMouseDoubleClick(sender, args);
                            // grid.Focus();
                            var check = grid.IsFocused;
                            int i = 1 + 1;
                        }
                    }
                }
    
            }

    Event is raised, whenever user is opening/closing/changing selection of tab in my tabcontrol. Code is taking this tab, it's content as a grid, and first element of grid of type RadGridView (telerik controlls, similar to GridView).

    Now I want to achieve following thing:

    User is changing tab and pressing F5 to refresh grids. Right now it works with additional step - User is changing tab, clicking on one of components, pressing F5 to refresh grids. I want to eliminate middle step from this scenario, so I decided to set focus on first RadGridView of my page, whenever user switches tab. I've tried to do this with 2 different methods:

    by grid.Focus();

    by raising grid.MouseDown event, which is focusing sender as RadGridView. 

    None of those works,  var check = grid.IsFocused is always false. How do I do it corectly?

    Monday, March 26, 2018 2:50 PM

All replies

  • You should realise that this forum is for Microsoft support only. Those controls with rad... in their name are telerik and hence outside of the scope of this forum.

    Maybe you could use focusmanager

    Something like:

    <TabItem   Header="...."
               FocusManager.FocusedElement="{Binding YourGridName}"
                >


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Monday, March 26, 2018 3:10 PM
    Moderator
  • You should realise that this forum is for Microsoft support only. Those controls with rad... in their name are telerik and hence outside of the scope of this forum.

    Maybe you could use focusmanager

    Something like:

    <TabItem   Header="...."
               FocusManager.FocusedElement="{Binding YourGridName}"
                >


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    So, for better understanding: If you replace telerik Rad Controlls, with Microsoft ones, focus still doesn't work. I've tested it. Anyway, your solution looks worth giving a shot, but in my case, I'm creating tabs dynamically, from code behind. Can't set FocusManager like that and also, different pages have different names.


    • Edited by Vanghern Tuesday, March 27, 2018 7:24 AM
    Tuesday, March 27, 2018 7:18 AM
  • Hi Vanghern,

    >>Can't set FocusManager like that and also, different pages have different names.

    You could set FocusManager.SetFocusedElement by programmatically

    // Sets the focused element in tabControl // tabControl is a TabControl.

    // button2 is the first element FocusManager.SetFocusedElement(tabControl, button2);

    For more information, please refer to:

    https://msdn.microsoft.com/en-us/library/system.windows.input.focusmanager.setfocusedelement%28v=vs.110%29.aspx

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 11, 2018 2:27 AM
    Moderator
  • Hi Vanghern,

    According to your code, you put grid in TabItem in TabControl, if you want to focus one control in grid, you can use FocusManager.FocusedElement Attached Property

    Like this:

     <TabItem Header="3">
                        <Grid FocusManager.FocusedElement="{Binding ElementName=textbox1}">
                            <TextBox x:Name="textbox1" Width="50" Height="50"></TextBox>
                        </Grid>
                    </TabItem>

    https://msdn.microsoft.com/en-us/library/system.windows.input.focusmanager.focusedelement(v=vs.110).aspx

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 11, 2018 6:58 AM
    Moderator