Issues with ReactiveCommand never enabling the button in WPF

530 views
Skip to first unread message

Peter Davis

unread,
Sep 2, 2014, 9:12:15 PM9/2/14
to reacti...@googlegroups.com
I must be doing something wrong as I cannot get a ReactiveCommand to enable at a later point in time. When I set the can execute IObservable to something that evaluates to false a lot to begin with, will never enable the bound button when the IObservable<bool> eventually returns true.

Simply I have the following simple command setup.

    var canApprove = this.WhenAny(x => x.CurrentJobStatus, x => x.JobId, (status, jobId) => status.Value == JobStatus.AwaitingApproval.Description  && jobId.Value != Guid.Empty);
    this.ApproveCommand = ReactiveCommand.CreateAsyncTask(canApprove, ApproveJob);

With the following properties on the ReactiveObject view model

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

When I subscribe to the canApprove observable, I see that it does eventually return true. However, when that happens the CanExecuteChanged event is not raised.
            canApprove.Subscribe(b => Debug.WriteLine("CanApprove: {0}", b));

With this subscription I normally see the following output and a disabled button:
CanApprove: False
CanApprove: False
CanApprove: True

I have substituted the canApprove observable for Observable.Return(true) to bypass for now, and it disables while running async no problem.

This is proving to be a bit of a show stopper for me.

Paul Betts

unread,
Sep 3, 2014, 2:15:53 AM9/3/14
to reacti...@googlegroups.com
Hey Peter,

I believe your problem is the canExecute Observable. You need to include “JobStatus.AwaitingApproval.Description” as an input into your WhenAny, or else your statement doesn’t evaluate correctly when JobStatus, AwaitingApproval, or Description changes.

Paul


--
You received this message because you are subscribed to the Google Groups "ReactiveUI mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reactivexaml...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Davis

unread,
Sep 3, 2014, 10:21:58 PM9/3/14
to reacti...@googlegroups.com
JobStatus.AwaitingApproval is a static instance of the JobStatus class, and its Description returns a constant string. So I dont think that is the cause?

I am seeing the observable return true as I demonstrated by subscribing and logging to Debug output.

The WPF button and binding is pretty simple:
    <Button Content="Approve" Margin="6,0,4,0" Width="72" Command="{Binding ApproveCommand}"/>

When I removed the canExecute, the button will disable while the async execute task is running, and then enables on completion.

It just will not enable when the initial state for canExecute is false.
Reply all
Reply to author
Forward
0 new messages