How to set focus on TextBox in Silverlight 4 out-of-browser popup

后端 未结 6 1035
后悔当初
后悔当初 2020-12-18 10:30

I have a simple ChildWindow popup in Silverlight 4 (beta).

Important: This is an out-of-browser application.

i want to auto

相关标签:
6条回答
  • 2020-12-18 10:37

    You are on the right track. You need to handle for two test cases:
    1. Setting the focus in the browser. 2. Setting the focus out of the browser.

    Your code you that you showed in the Loaded event will work perfectly fine out of the browser. All that is necessary is to refactor it to handle both cases:

    private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
    {
        if (App.current.IsRunningOutOfBrowser)
        {
            textBox1.Focus();
        }
        else
        {
            System.Windows.Browser.HtmlPage.Plugin.Focus();
            textBox1.Focus();
        }
    }
    

    That should do the trick for you.

    0 讨论(0)
  • 2020-12-18 10:43

    I use:

    protected override void OnOpened()
    {
        base.OnOpened();
        textBox1.Focus();
    }
    
    0 讨论(0)
  • 2020-12-18 10:50

    Thanks for all the posts but after doing a little research the below thing work for me

    in Xamal:

    <TextBox VerticalAlignment="Center" HorizontalAlignment="Left" FontFamily="Arial" FontSize="12" Height="25" Width="200" Margin="38,50,0,0" Name="txtUserName" Text="{Binding LoginInfo.UserName,Mode=TwoWay, NotifyOnValidationError=True}"  IsTabStop="True" TabIndex="1" ></TextBox>
    
    // Initialiazing Main Part View Model
    /// </summary>
    /// <param name="mainPartViewModel"></param>
    public ChildWindowLoginControl(MainPartViewModel mainPartViewModel)
    {
         InitializeComponent();
         this.DataContext = mainPartViewModel;
         System.Windows.Browser.HtmlPage.Plugin.Focus();
         this.GotFocus += (s, e) => { txtUserName.Focus(); };
    }
    
    0 讨论(0)
  • 2020-12-18 10:52

    I had to use your GotFocus way for Silverlight 3 application written in IronPython when I wanted to set focus in ChildWindow.

    0 讨论(0)
  • 2020-12-18 10:53

    I was having the same problem in SilverLight 4 (OOB) and I noticed that the tab sequence would set focus to a control that i could not see. What appears to be happening is the focus is being set to your control (first one in the tab sequence) and then for some reason the focus moves to the ContentControl (name ="content"), which (i think) is the parent of the child window.

    ContentControl by default has IsTabStop=true. see.... Why would I want IsTabStop set to true on a ContentControl? To set the ContentControl.IsTabStop = false for all ContentControls in your app, add this to your styles.xaml.

       <Style  TargetType="ContentControl" >
             <Setter Property="IsTabStop" Value="false"/>
        </Style>
    

    The same issue happens with the tab sequence on the MainPage. This style will also fix this.

    0 讨论(0)
  • 2020-12-18 11:02

    Thanks for all the post, but i have find the work done through following.

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (App.Current.IsRunningOutOfBrowser)
            {
                txtSalesOrderNo.Focus(); 
            }
            else
            {
                System.Windows.Browser.HtmlPage.Plugin.Focus();
                txtSalesOrderNo.Focus(); 
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题