Question

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.

Was it helpful?

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());
    }
}

OTHER TIPS

To wax the question will show my answer. I used attached properties consequence have the behavior I wanted.

MainView.xaml

  <TextBox x:Name="txtUser" 
           helper:FocusExtension.IsFocused="{Binding Path=IsUserFocus,Mode=TwoWay}" 
           Text="{Binding Path=User,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
           controls:TextboxHelper.Watermark="User"/>

MainViewModel.cs

 public MainViewModel()
    {

        this.WhenAny(vm => vm.IsUserFocus, x => false).Throttle(TimeSpan.FromSeconds(2)).Subscribe(x =>
        {
            IsUserFocus = true;
        });           
       
    }

I used a timer because when my application starts, it performs animation if set focus before it ends the style of the cursor that is white will not run it is going to gets its normal color is black.

Thank you very much!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top