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

user481758 picture user481758 · Jan 3, 2011 · Viewed 8.6k times · Source

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.

Answer

Bryan Anderson picture Bryan Anderson · Jan 3, 2011

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}" />