Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding the Enabled property of a Android button with the MvvmCross

I have a problem when I try to bind the "Enabled" property of my Android Button to a Boolean of my ViewModel using the MvvmCross framework and I really don't know the origin of it.

So I have a ViewModel which contains the two following properties :

private ProjectDetailDTO _projectDetail;
    public ProjectDetailDTO ProjectDetail
    {
        get { return this._projectDetail; }
        set 
        { 
            _projectDetail = value; 
            RaisePropertyChanged(() => ProjectDetail);
            RaisePropertyChanged(() => HasPicture);
        }
    }

    private bool _hasPicture;
    public bool HasPicture
    {
        get { return ((this.ProjectDetail != null) && !String.IsNullOrEmpty(this.ProjectDetail.Pictures)); }
        set { _hasPicture = value;
            RaisePropertyChanged(() => HasPicture); 
        }
    }

As you would understand, my button is bind to the HasPicture property. So I have the following code for my button in my .axml file :

<Button
    local:MvxLang="Text LblSeePicturesValue"
    local:MvxBind="Enabled HasPicture,Click ShowProjectPicturesCommand"
    android:id="@+id/buttonPictures"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

I don't think it's a ViewModel problem because my WP application works well with this code. In fact, my ProjectDetailDTO is filled by calling a web service, so by an asynchronous method. I think it's why when the binding is realized the HasPicture property has the false value. But with my ViewModel code, the HasPicture property should be updated when the ProjectDetailDTO is filled. Is there anything I did wrong in my Android View?

Thanks for any help !

like image 765
Alexandre D. Avatar asked Jul 04 '14 08:07

Alexandre D.


1 Answers

I think what you are seeing here is some interaction between ICommand.CanExecute and the Enabled property. There's a discussion about this on https://github.com/MvvmCross/MvvmCross/issues/729

To work around this, try switching the binding to:

local:MvxBind="Click ShowProjectPicturesCommand;Enabled HasPicture"

(Also note that the separator in bindings is a ; - not a ,)

like image 90
Stuart Avatar answered Sep 22 '22 16:09

Stuart