none
Select datagrid entry directly after it is constructed

    Question

  • Hi there,

    i build a datagrid and assign it to my Layoutroot.

    When the datagrid is displayed, i can`t directly navigate with the arrow keys of my keyboard, becuase no entry is selected.

    I need to click on at least one entry and then i can navigate with the keyboard.

    I want that after the datagrid is displayed i can directly navigate with the arrow keys

    I tried it by setting the focus on the datagrid after i added it to my Layoutroot, but this did not work.

    So what do to? I think this is a easy problem and there must be a simple solution for that, but until now i didn`t get it.

    Hope you can give some hints.

    thanks, alex

    DataGrid myGrid = new DataGrid(); 
    this.LayoutRoot.Children.Add(MyGrid);

     

    Friday, April 20, 2012 10:53 AM

Answers

  • Hi,

    I suggest you to set focus after the ui thread have done add operation.

    Code:

                
    Dispatcher.BeginInvoke(() => {
                    //Code
                });

    Hope helpful, If you still have the problem, please let me know.

    Tuesday, April 24, 2012 4:24 AM
  • Hi

    Please refer to the code below:

           
     public MainPage()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(MainPage_Loaded);
                dg.GotFocus += new RoutedEventHandler(dg_GotFocus);
            }
    
            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    System.Windows.Browser.HtmlPage.Plugin.Focus();
                    dg.Focus();
                });
            }
            void dg_GotFocus(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("hi");
            }

    You can find I add the code :

    System.Windows.Browser.HtmlPage.Plugin.Focus();
    

    This code is used to set silverlight plugin as focused in html page.

    Please have a try and hope helpful.

    Tuesday, April 24, 2012 10:04 PM
  • Hi alex87,

    It works fine in my computer.

    Do you use this.Loaded += new RoutedEventHandler(MainPage_Loaded); ?

    It will fired event time the page loaded.

    Or you can register different event and have a try.

    Such as dg.Loaded or LayoutRoot.Loaded.

    Or create a DispatcherTimer to call the focus method after a little time.

    Wednesday, April 25, 2012 9:13 PM

All replies

  • Hi,

    I suggest you to set focus after the ui thread have done add operation.

    Code:

                
    Dispatcher.BeginInvoke(() => {
                    //Code
                });

    Hope helpful, If you still have the problem, please let me know.

    Tuesday, April 24, 2012 4:24 AM
  • hi Otomii ,

      I tried with the following code snippets.

         LayoutRoot.LayoutUpdated += new EventHandler(LayoutRoot_LayoutUpdated);
             
        dg.GotFocus += new RoutedEventHandler(dg_GotFocus);
             
        dg.Focus();

              
                Dispatcher.BeginInvoke((Action)(() =>
                {
                    dg.SelectedIndex = 0;
                    dg.Focus();


                }));

                Dispatcher.BeginInvoke(() =>
                {

                    dg.SelectedIndex = 0;
                    dg.Focus();

                });

              
              void dg_GotFocus(object sender, RoutedEventArgs e)
            {
                MesageBox.Show("hi");
            }

    The Focus event is not getting executed , unless i click on the datagrid . 

    So , can we trigger a mouse click event as soon as the userControl gets loaded ? 

    Tuesday, April 24, 2012 6:03 AM
  • If you have entries directly after it is constructed try this

    mygrid = new DataGrid();

    mygrid.SelectedIndex = 0;

    Tuesday, April 24, 2012 11:51 AM
  • Hi

    Please refer to the code below:

           
     public MainPage()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(MainPage_Loaded);
                dg.GotFocus += new RoutedEventHandler(dg_GotFocus);
            }
    
            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    System.Windows.Browser.HtmlPage.Plugin.Focus();
                    dg.Focus();
                });
            }
            void dg_GotFocus(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("hi");
            }

    You can find I add the code :

    System.Windows.Browser.HtmlPage.Plugin.Focus();
    

    This code is used to set silverlight plugin as focused in html page.

    Please have a try and hope helpful.

    Tuesday, April 24, 2012 10:04 PM
  • Hi Otomii Lu,

    thats exactly what i wanted, thanks a lot!!!

    Works very well and focuses the silverlight plugin / datagrid after loading, nice.

     

    There is just one more point: When refreshing the browser (F5) it doesn`t get the focus again. It only works for the first time. Any ideas why?

    Wednesday, April 25, 2012 10:49 AM
  • Hi alex87,

    It works fine in my computer.

    Do you use this.Loaded += new RoutedEventHandler(MainPage_Loaded); ?

    It will fired event time the page loaded.

    Or you can register different event and have a try.

    Such as dg.Loaded or LayoutRoot.Loaded.

    Or create a DispatcherTimer to call the focus method after a little time.

    Wednesday, April 25, 2012 9:13 PM
  • Thanks Otomii Lu!

     thats ok for me.

    Thursday, April 26, 2012 6:52 AM