WPF - Set Focus when a button is clicked - No Code Behind

前端 未结 7 1299
小蘑菇
小蘑菇 2020-11-29 03:53

Is there a way to set Focus from one control to another using WPF Triggers?

Like the following example:



        
7条回答
  •  清歌不尽
    2020-11-29 04:16

    Look if you're using any Dispatcher then it would be helpful but this is a short trick I used in my code. Just use the Loaded event in your XAML and make a new handler. In that handler paste this code and bingo! you're ready to go

    Your loaded event with some arguments here...

    {
        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, new System.Action(() => {
            The element to be focused goes here......
        }));
    }
    

    PS: It requires Windows. Threading if you didn't know ;)

提交回复
热议问题