Skip to content

Instantly share code, notes, and snippets.

@nschoenberg
Last active August 29, 2015 14:15
Show Gist options
  • Save nschoenberg/f83418cb8575917e9d04 to your computer and use it in GitHub Desktop.
Save nschoenberg/f83418cb8575917e9d04 to your computer and use it in GitHub Desktop.
Cross platform CheckBox control using unicode signs, with some bindable properties
...
var checkBox = new CheckBox();
MainPage = new ContentPage() {
Content = checkBox
};
...
using System;
using Xamarin.Forms;
namespace F4M.Framework.Xma.Controls {
/// <summary>
/// The check box checked event args.
/// </summary>
public class CheckboxChangedEventArgs : EventArgs {
/// <summary>
/// Initializes a new instance of the <see cref="CheckboxChangedEventArgs"/> class.
/// </summary>
/// <param name="item">The Item. Use this as a tag</param>
/// <param name="isChecked">if set to <c>true</c> [new value].</param>
public CheckboxChangedEventArgs(object item, bool isChecked) {
IsChecked = isChecked;
Item = item;
}
/// <summary>
/// Gets the Item. Use it to reference anything you want. Works like a Tag
/// </summary>
/// <value>
/// The Item.
/// </value>
public object Item { get; private set; }
/// <summary>
/// Gets a value indicating whether this instance is checked.
/// </summary>
/// <value>
/// <c>true</c> if this instance is checked; otherwise, <c>false</c>.
/// </value>
public bool IsChecked { get; private set; }
}
/// <summary>
/// A typical checkbox control
/// </summary>
/// <remarks>
/// If you want to use this control in XAML, you have to define the namespace first.
/// <example>
/// Example page definition
/// <code lang="xml">
/// <![CDATA[
/// <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
/// xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
/// x:Class="F4M.AdapterDemo.Views.CheckboxSampleView"
/// xmlns:controls="clr-namespace:F4M.Framework.Xma.Controls;assembly=F4M.Framework.Xma">
/// <StackLayout>
/// <controls:Item IsChecked="true"/>
/// </StackLayout>
/// </ContentPage>
/// ]]>
/// </code>
/// </example>
/// </remarks>
public class CheckBox : ContentView {
private Label _checkboxControl;
private readonly string _checkOff;
private readonly string _checkOn;
#region IsChecked
/// <summary>
/// Occurs when control gets [checked].
/// </summary>
public event EventHandler<CheckboxChangedEventArgs> Checked;
/// <summary>
/// Gets or sets a value indicating whether this instance is checked.
/// </summary>
/// <value>
/// <c>true</c> if this instance is checked; otherwise, <c>false</c>.
/// </value>
public bool IsChecked {
get { return (bool)GetValue(IsCheckedProperty); }
set { SetValue(IsCheckedProperty, value); }
}
/// <summary>
/// The is checked property
/// </summary>
public static BindableProperty IsCheckedProperty = BindableProperty.Create<CheckBox, bool>(
o => o.IsChecked,
default(bool),
propertyChanged: OnIsCheckedPropertyChanged);
/// <summary>
/// Called when [IsChecked] property changed.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="oldValue">if set to <c>true</c> [old value].</param>
/// <param name="newValue">if set to <c>true</c> [new value].</param>
private static void OnIsCheckedPropertyChanged(BindableObject sender, bool oldValue, bool newValue) {
var checkBox = (CheckBox)sender;
var doCheck = newValue;
if (doCheck) {
checkBox.TransitionToChecked();
} else {
checkBox.TransitionToUnchecked();
}
if (checkBox.Checked != null) {
checkBox.Checked(checkBox, new CheckboxChangedEventArgs(checkBox.Item, doCheck));
}
}
#endregion
#region Item
/// <summary>
/// Gets or sets the item. Use this like a tag if you want to bind additional data
/// </summary>
/// <value>
/// The item.
/// </value>
public object Item {
get { return GetValue(ItemProperty); }
set { SetValue(ItemProperty, value); }
}
/// <summary>
/// The item property
/// </summary>
public static BindableProperty ItemProperty = BindableProperty.Create<CheckBox, object>(
o => o.Item,
default(object),
propertyChanged: OnItemPropertyChanged);
/// <summary>
/// Called when [Item] property changed.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="oldvalue">The old value.</param>
/// <param name="newvalue">The new value.</param>
private static void OnItemPropertyChanged(BindableObject sender, object oldvalue, object newvalue) {
}
#endregion
#region FontSize
/// <summary>
/// Gets or sets the size of the font.
/// </summary>
/// <value>
/// The size of the font.
/// </value>
public double FontSize {
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
/// <summary>
/// The font size property
/// </summary>
public static BindableProperty FontSizeProperty = BindableProperty.Create<CheckBox, double>(o => o.FontSize, -1.0, propertyChanged: OnFontSizePropertyChanged);
/// <summary>
/// Called when [FontSize] property changed.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="oldvalue">The old value.</param>
/// <param name="newvalue">The new value.</param>
private static void OnFontSizePropertyChanged(BindableObject sender, double oldvalue, double newvalue) {
var checkBox = (CheckBox) sender;
checkBox._checkboxControl.FontSize = newvalue;
}
#endregion
private void TransitionToChecked() {
_checkboxControl.Text = _checkOn;
}
private void TransitionToUnchecked() {
_checkboxControl.Text = _checkOff;
}
/// <summary>
/// Initialize a new instance of <see cref="CheckBox"/>.
/// </summary>
public CheckBox() {
_checkOn = GetCheckOnSymbol();
_checkOff = GetCheckOffSymbol();
InitCheckboxControl();
SetupView();
RegisterGestureRecognizers();
}
private string GetCheckOffSymbol() {
var result = "unknownCheckboxOffSymbol";
Device.OnPlatform(
Android: () => { result = "\u25FB"; },
Default: () => { result = "\u2610"; }
);
return result;
}
private string GetCheckOnSymbol() {
var result = "unknownCheckboxOffSymbol";
Device.OnPlatform(
Android: () => { result = "\u25FC"; },
Default: () => { result = "\u2611"; }
);
return result;
}
private void InitCheckboxControl() {
_checkboxControl = new Label { Text = IsChecked ? _checkOn : _checkOff };
}
private void SetupView() {
Content = _checkboxControl;
}
private void RegisterGestureRecognizers() {
var tabGesture = new TapGestureRecognizer();
tabGesture.Command = new Command(ToggleCheckbox);
_checkboxControl.GestureRecognizers.Add(tabGesture);
}
private void ToggleCheckbox() {
IsChecked = !IsChecked;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment