Note that there are some explanatory texts on larger screens.

plurals
  1. POReactive Extensions for .NET (Rx) in WPF - MVVM
    primarykey
    data
    text
    <p>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.</p> <p>If the last change of the Text property was more than 3 seconds ago I need to call the LoadUser method of a service.</p> <p>Porting the logic from my old solution to the new solution with MVVM architecture.</p> <h2>OLD</h2> <p><strong>XAML:</strong></p> <pre><code>&lt;TextBox Name="Nick" Grid.Row="0" FontSize="14" Margin="2,2,2,2" HorizontalAlignment="Stretch" TextChanged="Nick_TextChanged" /&gt; </code></pre> <p>In <strong>code behind</strong> I have this:</p> <pre><code>... Observable.FromEvent&lt;TextChangedEventArgs&gt;(Nick, "TextChanged") .Select(e =&gt; ((TextBox)e.Sender).Text) .Where(text =&gt; text.Length &gt; 3) .Do(LoadUser) .Throttle(TimeSpan.FromSeconds(3000)) .Subscribe(LoadUser); ... private void LoadUser(string text){...} </code></pre> <p>I would like use Observable.FromEvent in my view model class. Something like this</p> <h2>WPF with MVVM</h2> <p><strong>View:</strong></p> <pre><code>&lt;TextBox Name="Nick" Grid.Row="0" FontSize="14" Margin="2,2,2,2" HorizontalAlignment="Stretch" Micro:Message.Attach="[TextChanged]=[Action TextChanged()]"/&gt; </code></pre> <p><strong>View Model:</strong></p> <pre><code>[Export(typeof(IAddFriendViewModel))] public class AddFriendViewModel : Screen, IAddFriendViewModel { private string _nick; public string Nick { get { return _nick; } set { _nick = value; NotifyOfPropertyChange(()=&gt;Nick); } } ... //how can I access to textbox control Nick in view from view model class? Observable.FromEvent&lt;TextChangedEventArgs&gt;(Nick, "TextChanged") .Select(e =&gt; ((TextBox)e.Sender).Text) .Where(text =&gt; text.Length &gt; 3) .Do(LoadUser) .Throttle(TimeSpan.FromSeconds(3000)) .Subscribe(LoadUser); ... private void LoadUser(string text) { } public void TextChanged() { } } </code></pre> <p>My problem is Observable.FromEvent uses the TextBox control and I don’t know how can I access this control from my ViewModel class.</p> <p>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.</p> <p>Thank for ideas.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload