Last active
July 18, 2017 09:00
-
-
Save jt535/d000097fa7b1081743e683e43cf7952c to your computer and use it in GitHub Desktop.
a hideable toolbar item for xamarin forms apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage of the control
<customControls:HideableToolbarItem Text="Func" Command="{Binding MyCommand}" IsVisible="{Binding ShouldShowCommand}"/>