IsTabStop = False on a SL4 Text box

后端 未结 2 372
谎友^
谎友^ 2021-01-12 18:00

I set IsTabStop to false on a text box and I know that this makes the control unable to receive focus, but according to the Silverlight Forums, it should still be able to re

2条回答
  •  隐瞒了意图╮
    2021-01-12 18:30

    @seekerOfKnowledge: Disabling IsTabStop on the LostFocus is a good approach, but your re-focus hack is unnecessary. It fails to have any visible effect the first time around because the change of IsTabStop has not yet taken effect. This approach can be also be taken with any other control.

            var control = sender as Control;
            if (control != null)
            {
                control.MouseLeftButtonDown += (sender, args) =>
                    {   //This event fires even if the control isn't allowed focus. 
                        //As long as the control is visible, it's typically hit-testable.
                        if (!control.IsTabStop)
                        {
                            control.IsTabStop = true;
                            //threading required so IsTabStop change can take effect before assigning focus
                            control.Dispatcher.BeginInvoke(() =>
                                {
                                    control.Focus();
                                });
                        }
                    };
    
                control.LostFocus += (sender, args) =>
                    {   //Remove IsTabStop once the user exits the control
                        control.IsTabStop = false;
                    };
            }
    

提交回复
热议问题