using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Tango.Core.Commands; namespace Tango.Visuals { /// /// Represents an LED like button that acts like a check box. /// public partial class Led : UserControl { #region Constructors public Led() { InitializeComponent(); this.PreviewMouseUp += LedControl_PreviewMouseUp; } #endregion #region Event Handlers void LedControl_PreviewMouseUp(object sender, MouseButtonEventArgs e) { if (IsPassive) return; IsChecked = !IsChecked; if (Command != null) { Command.Execute(null); } } #endregion #region Properties /// /// Gets or sets whether the LED is on or off. /// public bool IsChecked { get { return (bool)GetValue(IsCheckedProperty); } set { SetValue(IsCheckedProperty, value); } } public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(Led), new PropertyMetadata(false)); /// /// Gets or sets whether the control is enabled. /// public bool Enabled { get { return (bool)GetValue(EnabledProperty); } set { SetValue(EnabledProperty, value); } } public static readonly DependencyProperty EnabledProperty = DependencyProperty.Register("Enabled", typeof(bool), typeof(Led), new PropertyMetadata(true, new PropertyChangedCallback(EnabledChanged))); private static void EnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Led l = d as Led; if ((bool)e.NewValue) { l.gridMask.Visibility = Visibility.Hidden; l.grid.Visibility = Visibility.Visible; } else { l.gridMask.Visibility = Visibility.Visible; l.grid.Visibility = Visibility.Hidden; } } public Brush FillBrush { get { return (Brush)GetValue(FillBrushProperty); } set { SetValue(FillBrushProperty, value); } } public static readonly DependencyProperty FillBrushProperty = DependencyProperty.Register("FillBrush", typeof(Brush), typeof(Led), new PropertyMetadata(null)); public double InnerOpacity { get { return (double)GetValue(InnerOpacityProperty); } set { SetValue(InnerOpacityProperty, value); } } public static readonly DependencyProperty InnerOpacityProperty = DependencyProperty.Register("InnerOpacity", typeof(double), typeof(Led), new PropertyMetadata(1.0)); public bool IsPassive { get { return (bool)GetValue(IsPassiveProperty); } set { SetValue(IsPassiveProperty, value); } } public static readonly DependencyProperty IsPassiveProperty = DependencyProperty.Register("IsPassive", typeof(bool), typeof(Led), new PropertyMetadata(false)); public Brush CheckedBrush { get { return (Brush)GetValue(CheckedBrushProperty); } set { SetValue(CheckedBrushProperty, value); } } public static readonly DependencyProperty CheckedBrushProperty = DependencyProperty.Register("CheckedBrush", typeof(Brush), typeof(Led), new PropertyMetadata(null,CheckBrushChanged)); private static void CheckBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = d as Led; control.Resources["inner"] = control.CheckedBrush; } public Brush UnCheckedBrush { get { return (Brush)GetValue(UnCheckedBrushProperty); } set { SetValue(UnCheckedBrushProperty, value); } } public static readonly DependencyProperty UnCheckedBrushProperty = DependencyProperty.Register("UnCheckedBrush", typeof(Brush), typeof(Led), new PropertyMetadata(null,UnCheckedBrushChanged)); private static void UnCheckedBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = d as Led; control.Resources["innerOff"] = control.UnCheckedBrush; } #endregion #region Commands /// /// Represents the command to bind for CanExecute event. /// public RelayCommand Command { get { return (RelayCommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(RelayCommand), typeof(Led), new PropertyMetadata(null, new PropertyChangedCallback(CommandChanged))); private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Led l = d as Led; if (l.Command != null) { l.Command.CanExecuteChanged += (x, y) => { l.IsEnabled = l.Command.CanExecute(null); }; } } #endregion } }