Binding in custom Toolbar not working

fsulserfsulser Member ✭✭
edited March 20 in Xamarin.Forms

I created a custom toolbarItem to be able to handle visibility after checking out other questions.
I'm doing it based on this resource: https://gist.github.com/JasonEdouardThompson/d000097fa7b1081743e683e43cf7952c

having in my xaml:

<ContentPage.ToolbarItems>
    <views:HideableToolbarItem Icon="reload.png" Text="Reload" Command="{Binding ReloadTapCommand}" IsVisible="true"/>
    <views:HideableToolbarItem Icon="add.png" Text="Add" Command="{Binding AddTapCommand}" IsVisible="{Binding CanAddComment}"/>
</ContentPage.ToolbarItems>

I have the problem that there is something not working with the binding, if I add a breakpoint to CanAddComment it is never reaching the get function in my ViewModel:

    public bool CanAddComment
    {
        get
        {
            return Settings.IsLoggedIn;
        }
    }

if I add an IsEnabled="{Binding CanAddComment}" to the views:HideableToolbarItem I'm reaching the breakpoint. What is wrong with my binding?

Best Answer

Answers

  • LuisDavidDelaCruzLuisDavidDelaCruz Member ✭✭✭
    edited March 20

    Hi @fsulser, In your ViewModel:

    public Command AddTapCommand{ get; set; }
    
    //In your constructor ViewModel
    public ViewModel()
    {   
        AddTapCommand= new Command(async () => await Method());
    }
    async Task Method()
    {
        await Application.Current.MainPage.Navigation.PushAsync(new NextPage());
    }
    

    And add in the same way the other Command, and use INotifyPropertyChanged Interface to change your bool value
    hope be useful for you :smile:

  • fsulserfsulser Member ✭✭
    edited March 20

    In my viewmodel I'm already having:

        public ICommand AddTapCommand { get; private set; }
        public ICommand ReloadTapCommand { get; private set; }
        public XXViewModel()
        {
            AddTapCommand = new Command(AddTappedAsync);
            ReloadTapCommand = new Command(ReloadTappedAsync);
        }
    

    and this is actually working for reload but not for Add.

    If I add:

    <ContentPage.ToolbarItems>
        <views:HideableToolbarItem Icon="reload.png" Text="Reload" Command="{Binding ReloadTapCommand}" IsVisible="true"/>
        <views:HideableToolbarItem Icon="add.png" Text="Add" Command="{Binding AddTapCommand}" IsVisible="{Binding CanAddComment}"/>
        <views:HideableToolbarItem Icon="add.png" Text="Add" Command="{Binding AddTapCommand}" IsVisible="true"/>
    </ContentPage.ToolbarItems>
    

    the last button click is working, the second isn't.
    But my main problem is that CanAddComment is not disabling when it is true

  • LuisDavidDelaCruzLuisDavidDelaCruz Member ✭✭✭

    @fsulser yes, you need to use INotifyPropertyChanged Interface , you can use the next link as reference:

    working with booleans values in MVVM
    1.- https://luisdavidxamshap.wordpress.com/2018/03/30/validacion-de-formulario-en-xamarin-forms-mvvm/

  • fsulserfsulser Member ✭✭
    edited March 21

    Ok my whole ViewModel looks already like this and it is not working.

    public class LivetickerViewModel : BaseViewModel
    {
        private bool _canAdd;
        public bool CanAddComment
        {
            get
            {
                return _canAdd;
            }
            set
            {
                _canAdd = value;
                OnPropertyChanged(nameof(CanAddComment));
            }
        }
        public ICommand AddTapCommand { get; private set; }
        public ICommand ReloadTapCommand { get; private set; }
    
        public LivetickerViewModel()
        {
            CanAddComment = Settings.IsLoggedIn;
            AddTapCommand = new Command(AddTappedAsync);
            ReloadTapCommand = new Command(ReloadTappedAsync);
        }
    
        private async void AddTappedAsync()
        {
        }
    
        private async void ReloadTappedAsync()
        {
        }
    }
    

    and BaseViewModel:

    public class BaseViewModel : INotifyPropertyChanged
    {
        public BaseViewModel()
        {
    
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName]string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs((propertyName)));
        }
    }
    

    I checked the link, but cannot see whatI should add as it implements already the interface INotifyPropertyChanged and OnPropertyChanged

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited March 24

    Have you added BindingContext in your page.xaml.cs?

    ...
    BindingContext = ViewModel ;
    ...
    
  • fsulserfsulser Member ✭✭
    Yes I have, otherwise Reload button would probably also not work
  • JarvanJarvan Member, Xamarin Team Xamurai
    edited March 26

    I've reproduced the code and change 'Settings.IsLoggedIn' to true. It works fine. You can check the value of 'Settings.IsLoggedIn' .

    public class LivetickerViewModel : BaseViewModel
    {
        ...
    
        public LivetickerViewModel()
        {
            //CanAddComment = Settings.IsLoggedIn;
    
            CanAddComment = true;
    
            AddTapCommand = new Command(AddTappedAsync);
            ReloadTapCommand = new Command(ReloadTappedAsync);
        }
        ...
    }
    
  • fsulserfsulser Member ✭✭

    Ok I tried again. No Idea why but it is now working, without any changes

Sign In or Register to comment.