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.

Était-ce utile?

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

Autres conseils

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!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top