Examples of how Silverlight initial controls get focus

Source: Internet
Author: User
Tags silverlight

This problem was originally encountered in my actual project, but this is actually the original project requirements, require a entry to make in the user name that text box focus, instead of using the mouse to click to get the focus, and then took some time to solve their own. I didn't pay much attention to the past, Unexpectedly in QQ Group also someone asked the same question, I want to write it out, so that you can notice, may be helpful later.

Let me illustrate with a simple example.

Goal: I want to make a textbox in the Silveright project start getting focus while not doing any click.

Xaml:

1 <  x:name= "LayoutRoot"  Background= "White">2          <  Name= "MyTextBox"  Height= "  horizontalalignment"  = "Left"3Margin= "145,67,0,0"  VerticalAlignment  = "Top"  Width= "/>"4      </ Grid >

C#:

1  Public Partial class Mainpage:usercontrol 2      {  3          public 4     5                          6This              new     7 8             9          void mainpage_loaded(object11           *                              

Here we call focus () This method wants to get the focus of the TextBox. But we run the project to see that the TextBox does not have or gets the focus.

My analysis at the time: 1) could be that the method call was unsuccessful, there was no focus, 2) The textbox might have been focused, but the focus of the operation was focused on somewhere else.

So I made a simple change in the background.

1  Public Partial classMainpage:usercontrol2      {3           PublicMainPage ()4          { 5 InitializeComponent ();6               This. Loaded + =NewRoutedeventhandler (mainpage_loaded);7          } 8 9          voidMainpage_loaded (Objectsender, RoutedEventArgs e)Ten          {  OneDispatcherTimer timer =NewDispatcherTimer (); ATimer. Interval = Timespan.frommilliseconds ( the);  -Timer. Tick + =NewEventHandler (Timer_tick); - timer. Start ();  the          }  -  -          voidTimer_tick (Objectsender, EventArgs e) -          {  +              if(Mytextbox.focus ()) -              {  +MessageBox.Show ("text box focused on success!"); A              }  at          }  -}

There is no focus.

I use a thread to keep looking, the result or pop-up message is "text box focus success!", so it seems that the focus () method return to True is successful, and even focus to other places, I have to focus on this textbox, should also be successful, But we still see this text box without focus, it seems I was wrong in the first two analyses.

I don't know if we found out that we just clicked anywhere else, and we suddenly found this text box focused, what's going on?

When we took it for granted that I was building a Silverlight project, thinking about everything from the Silverlight itself to the solution, but actually we went to see that this Silverlight is actually built on an ASPX page, is a plug-in form exists, may be inserted into the flash people know, placed on the page with a <object> tag in, then we may think of it was just focused on the page, and not focus on the plug-in, So we add a HtmlPage.Plugin.Focus (); The back is also changed into

1  Public MainPage () 2          {   3 4              this               new   5     6 7                    void Mainpage_loaded (object    8            910              One}                         

Focus on success.

Okay, so we'll find that success is focused, and the conclusion is that to focus on a control in Silverlight, you have to focus on the Silverlight plug-in first, We can focus on the Silverlight by clicking on the mouse. But when we first did not click to get the focus of the control it might be necessary to use this method. I hope we can help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.