[11911] | 1 | using System; |
---|
| 2 | using System.Collections.Generic; |
---|
| 3 | using System.Linq; |
---|
| 4 | using System.Text; |
---|
| 5 | using System.Windows.Data; |
---|
| 6 | using System.Windows.Markup; |
---|
| 7 | using System.Windows; |
---|
| 8 | |
---|
| 9 | namespace Com.Piwigo.Wpf.UI |
---|
| 10 | { |
---|
| 11 | public class SwitchBindingExtension : Binding |
---|
| 12 | { |
---|
| 13 | public SwitchBindingExtension() |
---|
| 14 | { |
---|
| 15 | Initialize(); |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | public SwitchBindingExtension(string path) |
---|
| 19 | : base(path) |
---|
| 20 | { |
---|
| 21 | Initialize(); |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | public SwitchBindingExtension(string path, object valueIfTrue, object valueIfFalse) |
---|
| 25 | : base(path) |
---|
| 26 | { |
---|
| 27 | Initialize(); |
---|
| 28 | this.ValueIfTrue = valueIfTrue; |
---|
| 29 | this.ValueIfFalse = valueIfFalse; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | private void Initialize() |
---|
| 33 | { |
---|
| 34 | this.ValueIfTrue = Binding.DoNothing; |
---|
| 35 | this.ValueIfFalse = Binding.DoNothing; |
---|
| 36 | this.Converter = new SwitchConverter(this); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | [ConstructorArgument("valueIfTrue")] |
---|
| 40 | public object ValueIfTrue { get; set; } |
---|
| 41 | |
---|
| 42 | [ConstructorArgument("valueIfFalse")] |
---|
| 43 | public object ValueIfFalse { get; set; } |
---|
| 44 | |
---|
| 45 | private class SwitchConverter : IValueConverter |
---|
| 46 | { |
---|
| 47 | public SwitchConverter(SwitchBindingExtension switchExtension) |
---|
| 48 | { |
---|
| 49 | _switch = switchExtension; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | private SwitchBindingExtension _switch; |
---|
| 53 | |
---|
| 54 | #region IValueConverter Members |
---|
| 55 | |
---|
| 56 | public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) |
---|
| 57 | { |
---|
| 58 | try |
---|
| 59 | { |
---|
| 60 | bool b = System.Convert.ToBoolean(value); |
---|
| 61 | return b ? _switch.ValueIfTrue : _switch.ValueIfFalse; |
---|
| 62 | } |
---|
| 63 | catch |
---|
| 64 | { |
---|
| 65 | return DependencyProperty.UnsetValue; |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) |
---|
| 70 | { |
---|
| 71 | return Binding.DoNothing; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | #endregion |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | } |
---|
| 78 | } |
---|