Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

IsEnabled on Buttons not being disabled upon initialization.

I have created a Xamarin Forms application using MVVM architecture. In my view model constructor I disable the buttons. However when the form loads up the are enabled. When I navigate through the form they respond appropriately. For example a check is done on the first entry but multiple entries are needed so the Complete button becomes disabled and enables when all entries are made.

I tried disabling them in the xaml constructor code behind but that ended up leaving them disabled all the time (not working at all).

Note: For the entry fields it works. They will be disabled upon form load.

        public ScanViewModel()
        {
            Title = "Scan Inventory";
            CompleteText = "SAVE";
            CancelText = "CANCEL";
            ErrorText = string.Empty;
            ShowError = false;

            ScanInventoryItemIsEnabled = true;
        ScanInventoryLocationIsEnabled = false;
        ScanInventoryQuantitysEnabled = false;

            CompleteEnabled = false;
            CancelEnabled = false;
        }

<StackLayout Orientation="Horizontal" Margin="10,5,10,0">
         <Button x:Name="ButtonComplete" Text="{Binding CompleteText}" IsEnabled="{Binding CompleteEnabled}" Command="{Binding Complete}"/>
          <Button x:Name="ButtonCancel" Text="{Binding CancelText}" IsEnabled="{Binding CancelEnabled}" Command="{Binding Complete}" />
</StackLayout>

Thank you.

Posts

  • RobertDavidsonRobertDavidson USMember ✭✭

    OK I found the answer here : https://stackoverflow.com/questions/28874883/button-isenabled-binding-not-working-properly

    In short in the Xaml the IsEnabled needs to come after the Command.

                    <StackLayout Orientation="Horizontal" Margin="10,5,10,0">
                        <Button x:Name="ButtonComplete" Text="{Binding CompleteText}" Command="{Binding Complete}" IsEnabled="{Binding CompleteEnabled}" />
                        <Button x:Name="ButtonCancel" Text="{Binding CancelText}"  Command="{Binding Complete}" IsEnabled="{Binding CancelEnabled}" />
                    </StackLayout>
    

    The enable status is the button was being overwritten by the Command's CanExecute.

  • JohnHardmanJohnHardman GBUniversity admin

    @RobertDavidson said:
    OK I found the answer here : https://stackoverflow.com/questions/28874883/button-isenabled-binding-not-working-properly

    In short in the Xaml the IsEnabled needs to come after the Command.

                    <StackLayout Orientation="Horizontal" Margin="10,5,10,0">
                        <Button x:Name="ButtonComplete" Text="{Binding CompleteText}" Command="{Binding Complete}" IsEnabled="{Binding CompleteEnabled}" />
                        <Button x:Name="ButtonCancel" Text="{Binding CancelText}"  Command="{Binding Complete}" IsEnabled="{Binding CancelEnabled}" />
                    </StackLayout>
    

    The enable status is the button was being overwritten by the Command's CanExecute.

    Which suggests that your CanExecute implementations are returning different values from your XXXXEnabled implementations. As you are binding Commands, it would be better to put the appropriate implementations in CanExecute and to get rid of the IsEnabled bindings altogether. That way you get the expected behavior without the risk of having two sets of logic that get out of synch.

  • RobertDavidsonRobertDavidson USMember ✭✭

    I appreciate that John Hardman. Thank you for the advice. I will do that.

Sign In or Register to comment.