开发者

focus does not enter targeted user control properly

开发者 https://www.devze.com 2023-01-25 18:30 出处:网络
I\'ve got a user control, consisting of a text box and two buttons. the control is placed on a dialog box and when i tab over the dialog

I've got a user control, consisting of a text box and two buttons.

the control is placed on a dialog box and when i tab over the dialog

controls, I experience proper behavior - first the text box is focused,

then one button, then the other.

However, when I set the user control as a target of a keyboard shortcut

set with "_" for a label (say press alt+r for "_Row count开发者_如何学Go") the user control

does not receive any focus. Tried implementing "gotkeyboardfocus" and setting

the focus to the textbox control there but it doesn't work.


A UserControl isn't focusable by default so you have to turn it on in order to get this to work.

<my:UserControl1 x:Name="userControl11" Focusable="True" .../>
<Label Target="userControl11">_Row count</Label>

And then you can focus the desired TextBox inside of the UserControl when it recieves Focus

private void UserControl_GotFocus(object sender, RoutedEventArgs e)
{
    textBox.Focus();
}


Hi I recently encountered focus problem. What I did is create a method called SetFocus() from inside my user control. Then inside that usercontrol I set the focus directly to my textbox control after calling .SetFocus() from consumer.

public void SetFocus()
{
this.txtCommand.Focus();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消