How to hide navigation Toolbar icon in xamarin?
Solution 1
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); });
}
}
}
Solution 2
As you have discovered yourself there is not IsVisible
. So you will have to implement functionality like that yourself if you still want it.
Another way would be to handle it in the pages' code-behind and remove or add the toolbar item whenever needed.
Adding and removing is simple, just add and remove items to the ToolbarItems
collection: ToolbarItems.RemoveAt(0);
for instance will remove the first toolbar item.
Solution 3
Putting @Gerald answer in action, it would be done this way:
void Done_Clicked(System.Object sender, System.EventArgs e)
{
//Do somthing and hide the done item
ShowDoneToolbarItem(false, (ToolbarItem)sender);
}
void Entry_Focused(System.Object sender, Xamarin.Forms.FocusEventArgs e)
{
//Show the done item
ShowDoneToolbarItem(true);
}
void ShowDoneToolbarItem(bool show, ToolbarItem item = null)
{
if(show)
{
ToolbarItem done = new ToolbarItem();
done.Text = "Done";
done.Clicked += Done_Clicked;
ToolbarItems.Add(done);
}
else if(item != null)
{
ToolbarItems.Remove(item);
}
}
This is cleaner and works from the code behind.
Comments
-
Maulik shah almost 2 years
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
please help me to sort out this issue.
-
Gerald Versluis almost 6 yearsUpdated my answer a bit. It isn't rocket science and not even Xamarin specific.
ToolbarItems
is just an array that you can access. -
Gerald Versluis almost 6 yearsI'm sorry, I don't know what you mean by that
-
Maulik shah almost 6 yearsi have create x:name="toolbaritem" in xaml and then access cs file but there is no property like "RemoveAt".
-
Maulik shah almost 6 yearshow can i hide toolbar item from model.in model i get a webservice response then i want to hide edit button according to response
-
Maulik shah almost 6 yearsI want to hide toolbar button using binding like isvisable ={BInding showItems}.
-
Gerald Versluis almost 6 yearsThat is not possible unfortunately
-
Maulik shah almost 6 yearshow can i hide toolbar item from model.in model i get a webservice response then i want to hide edit button according to response
-
Legion almost 4 yearsThank you very much!
-
thomasgalliker about 3 yearsThis is a very nice solution. However, keep in mind that the default visibility is "True" which means, you can toggle it using a binding like IsVisible="{Binding ShowToolbarItem}" from visible to hidden. With this approach, you cannot have the toolbar item hidden from the very beginning of the page lifetime (at least not with some flickering).
-
Nuri YILMAZ over 2 yearsQuestion; Why Xamarin team didn't implement IsVisible property?
-
Christoph Mett over 2 yearsWith this approach I pretty often get an InvalidOperationException stating "collection was modified; enumeration operation may not execute" at Page.OnBindingContextChanged. This seems to be the case because in Page.OnBindingContextChanged there is a foreach-loop iterating through the pages ToolbarItems and setting the new BindingContext which seems to interfere with the above approach to remove/add the ToolbarItems. Anyone has an idea how to fix that?
-
Christoph Mett over 2 yearsOkay, I fixed it by making it async and adding a Task.Delay before doing the adding/removing of the item.