Search code examples
wpfreactiveui

Focus on control using ReactiveUI?


In WPF, I started using ReactiveUI a short space of time is a question that came to me, it would be possible to put the focus on a specific control through my MainViewModel, it would be possible also use conditions?

Examples are welcome, thanks.


Solution

  • Here's the ReactiveUI Way to do it (since the question is pretty vague, here's a General Answer)

    public class MainViewModel
    {
        public ReactiveCommand Focusify { get; set; }
    
        public MainViewModel()
        {
            Focusify = new ReactiveCommand();
        }
    }
    
    public class MainView : UserControl, IViewFor<MainViewModel>
    {
        public MainView()
        {
            this.WhenAnyObservable(x => x.ViewModel.Focusify)
                .Subscribe(_ => someControl.Focus());
        }
    }