Set the focus on a textbox in xaml wpf

后端 未结 9 1060
走了就别回头了
走了就别回头了 2020-11-30 00:29

Despite some posts on this forum and others i cannot find something that tells me how to set the focus on a TextBox.

I have a userControl with many labe

9条回答
  •  甜味超标
    2020-11-30 00:37

    I have a TextBox inside a Grid inside a DataTemplate which I want to have keyboard focus when it becomes visible. I also found that

    
        
            
        
    
    

    did not work for me.

    However when I call Focus() in the parent ContentControl

    private void ContentControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if ((sender as ContentControl).IsVisible)
        {
            (sender as ContentControl).Focus();
        }
    }
    

    it starts to work and the caret is visible in the TextBox. I think the FocusScope has to be given focus for the FocusManager.FocusedElement property to have any effect.

    Jerry

提交回复
热议问题