Как настроить BindCommand для события MouseDown и соответствовать критериям MouseButtons.Right - PullRequest
0 голосов
/ 28 сентября 2018

Как настроить BindCommand для события MouseDown и соответствовать критериям MouseButtons.Right


Моя ViewModel

public class HomeViewModel : ReactiveObject
{
    private string _enteredText;

    private string _status;

    public HomeViewModel()
    {
        this.OK = ReactiveCommand.Create(() => { this.Status = this.Input + " is saved."; },
                                         this.WhenAny(p => p.Input,
                                                      s => !string.IsNullOrWhiteSpace(s.Value)));
    }

    public string Input
    {
        get { return this._enteredText; }
        set { this.RaiseAndSetIfChanged(ref this._enteredText, value); }
    }

    public string Status
    {
        get { return this._status; }
        set { this.RaiseAndSetIfChanged(ref this._status, value); }
    }

    public ReactiveCommand OK { get; }

    public void Run()
    {
        this.Status = this.Input + " is saved.";
    }
}

Моя форма

public partial class Form1 : Form, IViewFor<HomeViewModel>
{
    public Form1()
    {
        this.InitializeComponent();
        if (this.ViewModel == null)
        {
            this.ViewModel = new HomeViewModel();
        }

        this.Bind(this.ViewModel, x => x.Input, x => x.Input_TextBox.Text);
        this.Bind(this.ViewModel, x => x.Status, x => x.Status_ToolStripStatusLabel.Text);

        //this.BindCommand(this.ViewModel, x => x.OK, x => x.OK_Button);//ok
        //this.BindCommand(this.ViewModel, x => x.OK, x => x.OK_Button, nameof(MouseDown));//ok

        //this.BindCommand(this.ViewModel, x => x.OK, x => x.OK_Button, "MouseDown");//ok

        this.OK_Button
            .Events()
            .MouseDown
            .Where(p => p.Button == MouseButtons.Right)
            .InvokeCommand(this.ViewModel, p => p.OK);//fail

        this.OK_Button
            .Events()
            .MouseDown
            .Where(p => p.Button == MouseButtons.Right)
            .Subscribe(p => this.ViewModel.Run());//ok

    }

    object IViewFor.ViewModel
    {
        get { return this.ViewModel; }
        set { this.ViewModel = (HomeViewModel) value; }
    }

    public HomeViewModel ViewModel { get; set; }

    private void OK_Button_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            //TODO:Command 
        }
    }
}

Я надеюсь, BindCommand MouseButtons.Right Event.Поэтому я пытаюсь использовать следующий код:

this.OK_Button
    .Events()
    .MouseDown
    .Where(p => p.Button == MouseButtons.Right)
    .InvokeCommand(this.ViewModel, p => p.OK);

Получено исключение.

Я использую приведенный ниже код.Он может работать.Но я хочу использовать команду Bind Command

this.OK_Button
    .Events()
    .MouseDown
    .Where(p => p.Button == MouseButtons.Right)
    .Subscribe(p => this.ViewModel.Run());

Как настроить BindCommand для события MouseDown и соответствовать критериям MouseButtons.Right?

...