Search code examples
silverlightsilverlight-4.0wcf-ria-servicesria

button IsEnabled binding after asynchronous invoke call to server got completed


I tried to use InvokeOperation to get a boolean value from the DomainContext in my ViewModel.

As calls to the server are asynchronous, including invokes. I need to wait for the invoke to complete (i.e. ctx.epidemicContext().Completed += [an event handler]). And in invoke complete event I'm retriving the bool value and setting a property.

_context.IsChangeLogStarted(OnInvokeCompleted, null);

//OnInvoke complete set the property

private void OnInvokeCompleted(InvokeOperation<bool> invOp)
        {           
            IsChangeLogExist = invOp.Value;
        }

//Property

 public bool IsChangeLogExist
        {
            get { return this._IsChangeLogExist; }
            set { this._IsChangeLogExist = value; }
        }

And In my View I'm trying to bind the "IsChangeLogExist" to IsEnabled property of Button. //View

<Button IsEnabled="{Binding IsChangeLogExist}" Command="{Binding Source={StaticResource ProxyViewModel}, Path=StartChangeLogCommand}" CommandParameter="{Binding}" />

But the problem is when I run the application, Button's IsEnabled property is binding with default value (false) of IsChangeLogExist, With out setting its property value from the domaincontext due the Invoke's asynchronous operation. before invoke callback calls the OnInvokeCompleted, The button's Isenabled property is already getting the value. value is always false (since we are not setting the value and the invoke call is still at server)

Can some one suggest me how to bind IsEnabled property of button after getting the bool value from server and setting it in OnInvokeCompleted method.


Solution

  • You need to implement INotifyPropertyChanged in your view model and then raise this PopertyChanged event in setter of the property "IsChangeLogExists". This will refresh UI and new value of IsChangeLogExist will be used after callback completed.

    public bool IsChangeLogExist
    {
        get { return this._IsChangeLogExist; }
        set 
        { 
            this._IsChangeLogExist = value;
            if(PropertyChanged != null)
                  PropertyChanged(this, "IsChangeLogExists")
        }
    }