Set focus on a textbox control in UserControl in wpf

前端 未结 7 883
孤独总比滥情好
孤独总比滥情好 2020-12-05 07:46

I have created an UserControl which is loaded in a View (Window) in WPF. In my user control I have put a TextBox. I am unable to set focus on this

7条回答
  •  温柔的废话
    2020-12-05 08:46

    Another option that you have is to create a bool IsFocusedproperty in your view model. Then you can add a DataTrigger to set the focus when this property is true:

    In a Resources section:

    
    

    ...

    
    

    Note that at times, you may need to set it to false first to get it to focus (only when it is already true):

    IsFocused = false;
    IsFocused = true;
    

提交回复
热议问题