Skip to content

Instantly share code, notes, and snippets.

@jt535
Last active July 18, 2017 09:00
Show Gist options
  • Save jt535/d000097fa7b1081743e683e43cf7952c to your computer and use it in GitHub Desktop.
Save jt535/d000097fa7b1081743e683e43cf7952c to your computer and use it in GitHub Desktop.
a hideable toolbar item for xamarin forms apps
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace MyXamarinForms.CustomControls
{
/// <summary>
/// This is a hideable toolbar item for xaml views. It operates by setting the
/// text for the toolbar item to "" when it is invisible and removing the
/// command so that it can't be clicked on.
///
/// When the toolbar item is made visible, the original settings are applied.
/// </summary>
public class HideableToolbarItem : ToolbarItem
{
public bool IsVisible
{
get { return (bool)GetValue(IsVisibleProperty); }
set { SetValue(IsVisibleProperty, value); }
}
public static readonly BindableProperty IsVisibleProperty =
BindableProperty.Create(nameof(IsVisible),
typeof(bool),
typeof(HideableToolbarItem),
true,
propertyChanged:OnIsVisibleChanged);
private string oldText = "";
private System.Windows.Input.ICommand oldCommand = null;
private static void OnIsVisibleChanged(BindableObject bindable, object oldValue, object newValue)
{
var item = bindable as HideableToolbarItem;
var newValueBool = (bool)newValue;
var oldValueBool = (bool)oldValue;
if (!newValueBool && oldValueBool)
{
item.oldText = item.Text;
item.oldCommand = item.Command;
item.Text = "";
item.Command = null;
}
if(newValueBool && !oldValueBool){
item.Text = item.oldText;
item.Command = item.oldCommand;
}
}
}
}
@jt535
Copy link
Author

jt535 commented Jul 18, 2017

Usage of the control
<customControls:HideableToolbarItem Text="Func" Command="{Binding MyCommand}" IsVisible="{Binding ShouldShowCommand}"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment