using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Data; using System.Windows.Markup; using System.Windows; namespace Com.Piwigo.Wpf.UI { public class SwitchBindingExtension : Binding { public SwitchBindingExtension() { Initialize(); } public SwitchBindingExtension(string path) : base(path) { Initialize(); } public SwitchBindingExtension(string path, object valueIfTrue, object valueIfFalse) : base(path) { Initialize(); this.ValueIfTrue = valueIfTrue; this.ValueIfFalse = valueIfFalse; } private void Initialize() { this.ValueIfTrue = Binding.DoNothing; this.ValueIfFalse = Binding.DoNothing; this.Converter = new SwitchConverter(this); } [ConstructorArgument("valueIfTrue")] public object ValueIfTrue { get; set; } [ConstructorArgument("valueIfFalse")] public object ValueIfFalse { get; set; } private class SwitchConverter : IValueConverter { public SwitchConverter(SwitchBindingExtension switchExtension) { _switch = switchExtension; } private SwitchBindingExtension _switch; #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { try { bool b = System.Convert.ToBoolean(value); return b ? _switch.ValueIfTrue : _switch.ValueIfFalse; } catch { return DependencyProperty.UnsetValue; } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return Binding.DoNothing; } #endregion } } }