Xamarin Forms set button's IsEnabled Property by passing parameter

manikanthmanikanth INMember
edited March 2017 in Xamarin.Forms

Hello,

I would like to set a buttons enabled/disabled property by passing a parameter(an id), perform the comparison and set the button's enabled property. I have done this, and scratching my head since then.

My xaml page,

        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">

                                                <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="End" Padding="0,0,0,5">
                                                    <Button Text="Add Images" 
                                                            TextColor="Teal"
                                                            BackgroundColor="White" 
                                                            HorizontalOptions="FillAndExpand" 
                                                            VerticalOptions="Fill"
                                                            Command="{Binding Path=BindingContext.AddImagesCommand, Source={x:Reference dashboardPage}}"
                                                            CommandParameter="{Binding Id}"
                                IsEnabled="{Binding Path=BindingContext.IsDocketClosed, Source={x:Reference dashboardPage}}"/>
    />
                                                </StackLayout>
                                                <BoxView WidthRequest="1" VerticalOptions="End" HorizontalOptions="End" BackgroundColor="#DDD" />
                                            </StackLayout>

My corresponding VM,

    private Command<bool> isDocketClosed;
            public Command<bool> IsDocketClosed
            {
                get
                {
                    return IsDocketClosed ?? (isDocketClosed = new Command<bool>( CheckModelClosedDate, (bool isDocketClosed) => !IsBusy));
                }
            }

        public async Task<bool> CheckModelClosedDate(int modelId)
        {
            bool isModelClosed = false;
            await Task.Run(async () => {

                var db = await App.Database;
                var model= await db.GetModelAsync(modelId);

                if (model.DateClosed >= DateTime.Now)
                    isModelClosed = true;
                else
                    isModelClosed =  false;
            });
            return isModelClosed;
        }

Now my question is, how will I be able to pass on the model Id to VM's Command? Am I heading in the right direction, can you add in your thoughts..

Thanks.

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited March 2017

    You can't bind a Command<bool> to a bool property. This is how I would do it:

    private bool isDocketClosed;
    public bool IsDocketClosed
    {
        get
        {
            return isDocketClosed;
        }
        set
        {
            isDocketClosed = value;
            OnPropertyChanged();
        }
    }
    
    public async Task CheckModelClosedDate(int modelId)
    {
        var db = await App.Database;
        var model = await db.GetModelAsync(modelId);
    
        if (model.DateClosed >= DateTime.Now)
            IsDocketClosed = true;
        else
            IsDocketClosed =  false;
    }
    
  • MichalStaszewskiMichalStaszewski USMember ✭✭

    In VM both bool for IsEnabled & Id should raise OnPropertyChanged() in setter. In VM constructor this.PropertyChanged() - if property name ="Id" do your logic for setting IsEnabled to true/false. Bind button to IsEnabled on Page. No need for CommandParameter imho

Sign In or Register to comment.