Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide navigation Toolbar icon in xamarin?

I want to hide navigation bar button in xamarin. how can i do that using binding. Toolbar item doesn't have "IsVisible" property.

Following is my xaml code

Xaml code of navigation bar button

please help me to sort out this issue.

like image 503
Maulik shah Avatar asked May 31 '18 07:05

Maulik shah


1 Answers

I would suggest to build a bindable ToolBoxItem. That way you can control the visibility through a view model property.

An implementation could look like that:

public class BindableToolbarItem : ToolbarItem
{
    public static readonly BindableProperty IsVisibleProperty = BindableProperty.Create(nameof(IsVisible), typeof(bool), typeof(BindableToolbarItem), true, BindingMode.TwoWay, propertyChanged: OnIsVisibleChanged);

    public bool IsVisible
    {
        get => (bool)GetValue(IsVisibleProperty);
        set => SetValue(IsVisibleProperty, value);
    }

    private static void OnIsVisibleChanged(BindableObject bindable, object oldvalue, object newvalue)
    {
        var item = bindable as BindableToolbarItem;

        if (item == null || item.Parent == null)
            return;

        var toolbarItems = ((ContentPage)item.Parent).ToolbarItems;

        if ((bool)newvalue && !toolbarItems.Contains(item))
        {
            Device.BeginInvokeOnMainThread(() => { toolbarItems.Add(item); });
        }
        else if (!(bool)newvalue && toolbarItems.Contains(item))
        {
            Device.BeginInvokeOnMainThread(() => { toolbarItems.Remove(item); });
        }
    }
}
like image 140
tequila slammer Avatar answered Dec 28 '22 06:12

tequila slammer