Skip to content

Instantly share code, notes, and snippets.

@dayneo
Created March 16, 2016 17:06
Show Gist options
  • Select an option

  • Save dayneo/b8d6467acf742e46b565 to your computer and use it in GitHub Desktop.

Select an option

Save dayneo/b8d6467acf742e46b565 to your computer and use it in GitHub Desktop.
Adds ability to perform bitwise operations using a checkbox. (WPF)
using System;
using System.Windows;
using System.Windows.Controls;
/// <summary>
/// Adds ability to perform bitwise operations using a checkbox.
/// </summary>
public class BitFlagBehaviour : DependencyObject
{
/// <summary>
/// Returns true if the value contains the checked value. If either are null, then it returns null.
/// Otherwise it return false.
/// </summary>
/// <param name="value"></param>
/// <param name="checkedValue"></param>
/// <returns></returns>
static bool? IsChecked(int? value, int? checkedValue)
{
return (value & checkedValue) == checkedValue;
}
/// <summary>
/// Sets the checked value onto the value by performing a bitwise Or operation.
/// </summary>
/// <param name="value"></param>
/// <param name="checkedValue"></param>
/// <returns></returns>
static int? SetValue(int? value, int? checkedValue)
{
return (value | checkedValue);
}
/// <summary>
/// Unsets the checked value by performing a bitwize And Not operation.
/// </summary>
/// <param name="value"></param>
/// <param name="checkedValue"></param>
/// <returns></returns>
static int? UnsetValue(int? value, int? checkedValue)
{
return (value & ~checkedValue);
}
/// <summary>
/// Extracts the value and checkedvalue from the checkbox.
/// </summary>
/// <param name="element"></param>
/// <param name="value"></param>
/// <param name="checkedValue"></param>
static void GetValues(CheckBox element, out int? value, out int? checkedValue)
{
value = null;
checkedValue = null;
if (element.GetValue(BitFlagBehaviour.ValueProperty) != null)
{
value = (int)element.GetValue(BitFlagBehaviour.ValueProperty);
}
if (element.GetValue(BitFlagBehaviour.CheckedValueProperty) != null)
{
checkedValue = (int)element.GetValue(BitFlagBehaviour.CheckedValueProperty);
}
}
#region Value property implementation
public static readonly DependencyProperty ValueProperty = DependencyProperty.RegisterAttached("Value"
, typeof(object), typeof(BitFlagBehaviour)
, new PropertyMetadata(null, BitFlagBehaviour.Value_Changed));
public static object GetValue(CheckBox element)
{
return element.GetValue(BitFlagBehaviour.ValueProperty);
}
public static void SetValue(CheckBox element, object value)
{
element.SetValue(BitFlagBehaviour.ValueProperty, value);
}
static void Value_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CheckBox element = (CheckBox)d;
int? value, checkedValue;
BitFlagBehaviour.GetValues(element, out value, out checkedValue);
element.IsChecked = BitFlagBehaviour.IsChecked(value, checkedValue);
}
#endregion
#region CheckedValue property implementation
public static readonly DependencyProperty CheckedValueProperty = DependencyProperty.RegisterAttached("CheckedValue"
, typeof(object), typeof(BitFlagBehaviour)
, new PropertyMetadata(null, BitFlagBehaviour.CheckedValue_Changed));
public static object GetCheckedValue(CheckBox element)
{
return element.GetValue(BitFlagBehaviour.CheckedValueProperty);
}
public static void SetCheckedValue(CheckBox element, object value)
{
element.SetValue(BitFlagBehaviour.CheckedValueProperty, value);
}
static void CheckedValue_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CheckBox element = (CheckBox)d;
element.Checked -= CheckBox_Checked;
element.Checked += CheckBox_Checked;
element.Unchecked -= CheckBox_Unchecked;
element.Unchecked += CheckBox_Unchecked;
}
static void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
CheckBox element = (CheckBox)sender;
int? value, checkedValue;
BitFlagBehaviour.GetValues(element, out value, out checkedValue);
element.SetValue(BitFlagBehaviour.ValueProperty, BitFlagBehaviour.UnsetValue(value, checkedValue));
}
static void CheckBox_Checked(object sender, RoutedEventArgs e)
{
CheckBox element = (CheckBox)sender;
int? value, checkedValue;
BitFlagBehaviour.GetValues(element, out value, out checkedValue);
element.SetValue(BitFlagBehaviour.ValueProperty, BitFlagBehaviour.SetValue(value, checkedValue));
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment