Reactive Extensions for .NET (Rx) in WPF - MVVM
Asked Answered
U

1

12

I am using Reactive extensions for NET (Rx) with Caliburn.Micro in my WPF app. I'm trying to port my WPF app to use an MVVM architecture and I need to monitor changes in the Text property of a TextBox control.

If the last change of the Text property was more than 3 seconds ago I need to call the LoadUser method of a service.

Porting the logic from my old solution to the new solution with MVVM architecture.

OLD

XAML:

<TextBox Name="Nick" 
         Grid.Row="0"
         FontSize="14"
         Margin="2,2,2,2" 
         HorizontalAlignment="Stretch" 
         TextChanged="Nick_TextChanged" />

In code behind I have this:

...

Observable.FromEvent<TextChangedEventArgs>(Nick, "TextChanged")
          .Select(e => ((TextBox)e.Sender).Text)
          .Where(text => text.Length > 3)               
          .Do(LoadUser)     
          .Throttle(TimeSpan.FromSeconds(3000))     
          .Subscribe(LoadUser);

...
private  void LoadUser(string text){...}

I would like use Observable.FromEvent in my view model class. Something like this

WPF with MVVM

View:

<TextBox Name="Nick" 
         Grid.Row="0"
         FontSize="14"
         Margin="2,2,2,2" 
         HorizontalAlignment="Stretch"
         Micro:Message.Attach="[TextChanged]=[Action TextChanged()]"/>

View Model:

[Export(typeof(IAddFriendViewModel))]
public class AddFriendViewModel : Screen, IAddFriendViewModel
{
    private string _nick;

    public string Nick
    {
        get { return _nick; }
        set
        {
            _nick = value;
            NotifyOfPropertyChange(()=>Nick);
        }
    }

    ...

    //how can I access to textbox control Nick in view from view model class?
    Observable.FromEvent<TextChangedEventArgs>(Nick, "TextChanged")
              .Select(e => ((TextBox)e.Sender).Text)
              .Where(text => text.Length > 3)
              .Do(LoadUser)
              .Throttle(TimeSpan.FromSeconds(3000))
              .Subscribe(LoadUser);
    ...

    private void LoadUser(string text)
    { }

    public void TextChanged()
    {
    }
}

My problem is Observable.FromEvent uses the TextBox control and I don’t know how can I access this control from my ViewModel class.

I can bind to the Text property of TextBox or I can bind a command to some event of the TextBox control, but this method uses an object in the View.

Thank for ideas.

Unanimity answered 3/1, 2011 at 14:5 Comment(1)
That doesn't really answer the question, but I suggest you look at ReactiveUI. All that stuff you need is already implemented there.Magazine
M
27

Add a Nick property to your ViewModel and implement INotifyPropertyChanged. Then you can do this

Observable.FromEventPattern<PropertyChangedEventArgs>(this, "PropertyChanged")
          .Where(e => e.EventArgs.PropertyName == "Nick")
          .Select(_ => this.Nick)
          .Where(text => text.Length > 3)
          //Subscribe will call LoadUser, no need for the extra Do(...)
          .Throttle(TimeSpan.FromSeconds(3000))
          .Subscribe(LoadUser);  

and then your XAML would be something like this

<TextBox Name="Nick" 
         Grid.Row="0"
         FontSize="14"
         Margin="2,2,2,2" 
         HorizontalAlignment="Stretch"
         Text="{Binding Nick, UpdateSourceTrigger=PropertyChanged}" />
Michael answered 3/1, 2011 at 15:53 Comment(1)
Note in the above code FromEvent is now FromEventPattern (FromEvent requires explicit wiring up).Bailee

© 2022 - 2024 — McMap. All rights reserved.