| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Linq; |
| 4 | using System.Text; |
| 5 | using System.Threading.Tasks; |
| 6 | using System.Windows; |
| 7 | using System.Windows.Controls; |
| 8 | |
| 9 | namespace UI.Controls.Base |
| 10 | { |
| 11 | public class View : ContentControl |
| 12 | { |
| 13 | public string Condition |
| 14 | { |
| 15 | get { return (string)GetValue(ConditionProperty); } |
| 16 | set { SetValue(ConditionProperty, value); } |
| 17 | } |
| 18 | public static readonly DependencyProperty ConditionProperty = |
| 19 | DependencyProperty.Register("Condition", |
| 20 | typeof(string), |
| 21 | typeof(View)); |
| 22 | public object Value |
| 23 | { |
| 24 | get { return (object)GetValue(ValueProperty); } |
| 25 | set { SetValue(ValueProperty, value); } |
| 26 | } |
| 27 | public static readonly DependencyProperty ValueProperty = |
| 28 | DependencyProperty.Register("Value", |
| 29 | typeof(object), |
| 30 | typeof(View), new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged))); |
| 31 | |
| 32 | private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 33 | { |
| 34 | var control = d as View; |
| 35 | control.Handle(); |
| 36 | } |
| 37 | |
| 38 | public View() |
| 39 | { |
| 40 | DefaultStyleKey = typeof(View); |
| 41 | |
| 42 | Loaded += View_Loaded; |
| 43 | Unloaded += View_Unloaded; |
| 44 | } |
| 45 | |
| 46 | private void View_Unloaded(object sender, RoutedEventArgs e) |
| 47 | { |
| 48 | Loaded -= View_Loaded; |
| 49 | Unloaded -= View_Unloaded; |
| 50 | } |
| 51 | |
| 52 | private void View_Loaded(object sender, RoutedEventArgs e) |
| 53 | { |
| 54 | Handle(); |
| 55 | } |
| 56 | |
| 57 | private void Handle() |
| 58 | { |
| 59 | try |
| 60 | { |
| 61 | if (string.IsNullOrEmpty(Condition) && Value == null) |
| 62 | { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | bool isShow = false; |
| 67 | if (string.IsNullOrEmpty(Condition)) |
| 68 | { |
| 69 | if ((bool)Value) |
| 70 | { |
| 71 | isShow = true; |
| 72 | } |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | |
| 77 | |
| 78 | if (Condition.IndexOf("=") != -1) |
| 79 | { |
| 80 | string conditionVal = Condition.Substring(Condition.IndexOf("=") + 1); |
| 81 | string value = (Value == null ? string.Empty : Value.ToString()); |
| 82 | isShow = Condition.Contains("!") ? conditionVal != value : conditionVal == value; |
| 83 | } |
| 84 | else if (Condition.IndexOf("not null") != -1) |
| 85 | { |
| 86 | |
| 87 | isShow = Value != null; |
| 88 | } |
| 89 | else if (Condition.IndexOf("null") != -1) |
| 90 | { |
| 91 | |
| 92 | isShow = Value == null; |
| 93 | } |
| 94 | else if (Condition.IndexOf("not empty") != -1) |
| 95 | { |
| 96 | if (Value == null) |
| 97 | { |
| 98 | isShow = false; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | var data = Value as IEnumerable<object>; |
| 103 | if (data != null) |
| 104 | { |
| 105 | isShow = data.Count() > 0; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | isShow = !string.IsNullOrEmpty(Value.ToString()); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | else if (Condition.IndexOf("empty") != -1) |
| 114 | { |
| 115 | if (Value == null) |
| 116 | { |
| 117 | isShow = true; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | var data = Value as IEnumerable<object>; |
| 122 | if (data != null) |
| 123 | { |
| 124 | isShow = data.Count() == 0; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | isShow = string.IsNullOrEmpty(Value.ToString()); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | isShow = Condition == (Value != null ? Value.ToString() : ""); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | |
| 139 | |
| 140 | if (isShow) |
| 141 | { |
| 142 | Visibility = Visibility.Visible; |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | Visibility = Visibility.Collapsed; |
| 147 | } |
| 148 | } |
| 149 | catch (Exception ex) |
| 150 | { |
| 151 | Visibility = Visibility.Collapsed; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |