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.