Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Using Rx with MVVM pattern

Using Rx with MVVM pattern

Scheduled Pinned Locked Moved WPF
wpfregexarchitecturequestion
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kenneth Haugland
    wrote on last edited by
    #1

    I have a canvas which I hook to the ViewModel:

    With the ViewModel:

    class MainViewModel
    {
    public MainViewModel()
    {
    this.MouseDown = new DelegateCommand(
    this.OnMouseDown, this.CanMouseDown);
    }

        public ICommand MouseDown { get; private set; }
    
        private void OnMouseDown(object arg)
        {
        //Do Something
        }
        private bool CanMouseDown(object arg) { return true; }
    }
    

    But I really don't need all these details, I just need the positions received:

    var MouseDownPosition = from evt in Observable.FromEventPattern(
    h => cnvMainView.MouseDown += h,
    h => cnvMainView.MouseDown -= h)
    select evt.EventArgs.GetPosition(cnvMainView);

    So how do people hook the Rx observables to the ViewModel?

    K 1 Reply Last reply
    0
    • K Kenneth Haugland

      I have a canvas which I hook to the ViewModel:

      With the ViewModel:

      class MainViewModel
      {
      public MainViewModel()
      {
      this.MouseDown = new DelegateCommand(
      this.OnMouseDown, this.CanMouseDown);
      }

          public ICommand MouseDown { get; private set; }
      
          private void OnMouseDown(object arg)
          {
          //Do Something
          }
          private bool CanMouseDown(object arg) { return true; }
      }
      

      But I really don't need all these details, I just need the positions received:

      var MouseDownPosition = from evt in Observable.FromEventPattern(
      h => cnvMainView.MouseDown += h,
      h => cnvMainView.MouseDown -= h)
      select evt.EventArgs.GetPosition(cnvMainView);

      So how do people hook the Rx observables to the ViewModel?

      K Offline
      K Offline
      Kenneth Haugland
      wrote on last edited by
      #2

      I gave up on the Rx part and did my own EventCommand:

      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Input;
      using System.Windows.Interactivity;

      namespace WpfSimpleReflectionAssistant
      {
      //Code from:
      //https://social.msdn.microsoft.com/Forums/silverlight/en-US/5cd586e7-640f-447b-9040-e9270173abf7/passing-drop-event-data-in-a-command-parameter-using-mvvm-and-the-interactivity-framework?forum=silverlightmvvm
      public sealed class EventCommand : TriggerAction
      {

          public static readonly DependencyProperty CommandParameterProperty =
              DependencyProperty.Register("CommandParameter", typeof(object), typeof(EventCommand), null);
      
      
          public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
              "Command", typeof(ICommand), typeof(EventCommand), null);
      
      
          public static readonly DependencyProperty InvokeParameterProperty = DependencyProperty.Register(
              "InvokeParameter", typeof(object), typeof(EventCommand), null);
      
          private string commandName;
      
          public object InvokeParameter
          {
              get
              {
                  return this.GetValue(InvokeParameterProperty);
              }
              set
              {
                  this.SetValue(InvokeParameterProperty, value);
              }
          }
      
      
          public ICommand Command
          {
              get
              {
                  return (ICommand)this.GetValue(CommandProperty);
              }
              set
              {
                  this.SetValue(CommandProperty, value);
              }
          }
      
          public string CommandName
          {
              get
              {
                  return this.commandName;
              }
              set
              {
                  if (this.CommandName != value)
                  {
                      this.commandName = value;
                  }
              }
          }
      
          public object CommandParameter
          {
              get
              {
                  return this.GetValue(CommandParameterProperty);
              }
      
              set
              {
                  this.SetValue(CommandParameterProperty, value);
              }
          }
      
          public object Sender { get; set; }
      
          protected override void Invoke(object parameter)
          {
              this.InvokeParameter = parameter;
              if (this.AssociatedObject != null)
              {
                  ICommand command
      
      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups