PropertyBinding.cs 12.4 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections;

namespace Misc
{
    public class BindingOperations
    {
12

潘栩锋's avatar
潘栩锋 committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
        // 摘要:
        //     描述绑定中数据流的方向。
        public enum BindingMode
        {
            // 摘要:
            //     导致对源属性或目标属性的更改可自动更新对方。此绑定类型适用于可编辑窗体或其他完全交互式 UI 方案。
            TwoWay = 0,
            //
            // 摘要:
            //     当绑定源(源)更改时,更新绑定目标(目标)属性。如果要绑定的控件为隐式只读控件,则适用此绑定类型。例如,可以绑定到如股市代号之类的源。或者,可能目标属性没有用于进行更改(例如表的数据绑定背景色)的控件接口。如果不需要监视目标属性的更改,则使用
            //     System.Windows.Data.BindingMode.OneWay 绑定模式可避免 System.Windows.Data.BindingMode.TwoWay
            //     绑定模式的系统开销。
            OneWay = 1
        }
        /// <summary>
        /// 当绑定源(源)更改时,更新绑定目标(目标)属性
        /// </summary>
        /// <param name="src"></param>
        /// <param name="srcPropertyName"></param>
        /// <param name="target"></param>
        /// <param name="targetPropertyName"></param>
34
        public static void SetBinding(INotifyPropertyChanged src, string srcPropertyName, INotifyPropertyChanged target, string targetPropertyName)
潘栩锋's avatar
潘栩锋 committed
35 36 37
        {
            SetBinding(src, srcPropertyName, target, targetPropertyName, BindingMode.OneWay);
        }
38
        public static void SetBinding(INotifyPropertyChanged src, string srcPropertyName, IList target, int index)
潘栩锋's avatar
潘栩锋 committed
39 40 41 42 43 44 45
        {

            Type type_s = src.GetType();
            Type type_t = target.GetType();
            System.Reflection.PropertyInfo pi_s = type_s.GetProperty(srcPropertyName);
            if (pi_s == null)
                return;
46 47

            object obj = pi_s.GetValue(src, null);
潘栩锋's avatar
潘栩锋 committed
48 49 50 51 52 53 54 55 56 57
            target[index] = obj;

            src.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == srcPropertyName)
                {
                    object o = pi_s.GetValue(src, null);
                    target[index] = obj;
                }
            };
58

潘栩锋's avatar
潘栩锋 committed
59
        }
60
        public static void SetBinding(INotifyPropertyChanged src, string srcPropertyName, Action func)
潘栩锋's avatar
潘栩锋 committed
61 62 63 64
        {
            func();
            src.PropertyChanged += (s, e) =>
            {
65
                if (srcPropertyName == e.PropertyName)
潘栩锋's avatar
潘栩锋 committed
66 67 68 69 70
                {
                    func();
                }
            };
        }
71
        public static void SetBinding(INotifyPropertyChanged src, string[] srcPropertyNames, Action func)
潘栩锋's avatar
潘栩锋 committed
72 73 74 75
        {
            func();
            src.PropertyChanged += (s, e) =>
            {
76
                if (srcPropertyNames.Contains(e.PropertyName))
潘栩锋's avatar
潘栩锋 committed
77 78 79 80 81
                {
                    func();
                }
            };
        }
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

        public static void SetBinding(INotifyPropertyChanged src, string srcPropertyName, object target, Action func)
        {
            func();
            PropertyChangedEventHandler pcevent;
            src.PropertyChanged += pcevent = (s, e) =>
            {
                if (srcPropertyName == e.PropertyName)
                {
                    func();
                }
            };
            if (IsMarkdownEvents)
            {
                if (!bindingConexts.ContainsKey(target))
                {
                    bindingConexts.Add(target, new List<PropertyChangedEventContexts>());
                }
                var list = bindingConexts[target];
                list.Add(new PropertyChangedEventContexts() { src = src, propertyChanged = pcevent });
            }
        }
        public static void SetBinding(INotifyPropertyChanged src, string[] srcPropertyNames, object target, Action func)
        {
            func();
            PropertyChangedEventHandler pcevent;
            src.PropertyChanged += pcevent = (s, e) =>
            {
                if (srcPropertyNames.Contains(e.PropertyName))
                {
                    func();
                }
            };

            if (IsMarkdownEvents)
            {
                if (!bindingConexts.ContainsKey(target))
                {
                    bindingConexts.Add(target, new List<PropertyChangedEventContexts>());
                }
                var list = bindingConexts[target];
                list.Add(new PropertyChangedEventContexts() { src = src, propertyChanged = pcevent });
            }
        }
        public class PropertyChangedEventContexts
        {
            public INotifyPropertyChanged src;
            public PropertyChangedEventHandler propertyChanged;
        }
        static Dictionary<object, List<PropertyChangedEventContexts>> bindingConexts;
        static bool IsMarkdownEvents = false;
        /// <summary>
        /// 开始记录事件
        /// </summary>
        public static void StartMarkdownEvents(Dictionary<object, List<PropertyChangedEventContexts>> bindingConexts)
        {
            IsMarkdownEvents = true;
            BindingOperations.bindingConexts = bindingConexts;
        }
        /// <summary>
        /// 停止记录事件
        /// </summary>
        public static void StopMarkdownEvents()
        {
            IsMarkdownEvents = false;
            BindingOperations.bindingConexts = null;
        }

        /// <summary>
        /// 释放事件目标对象
        /// </summary>
        /// <param name="src"></param>
        public static void DisposeEventTargetObject(Dictionary<object, List<PropertyChangedEventContexts>> bindingConexts, object target)
        {
            if (!bindingConexts.ContainsKey(target))
                return;
            var list = bindingConexts[target];
            foreach (var context in list)
            {
                context.src.PropertyChanged -= context.propertyChanged;
            }
            bindingConexts.Remove(target);
        }
        /// <summary>
        /// 释放事件目标对象
        /// </summary>
        /// <param name="src"></param>
        public static void DisposeEventTargetObject(Dictionary<object, List<PropertyChangedEventContexts>> bindingConexts)
        {
            foreach (var list in bindingConexts.Values)
            {
                foreach (var context in list)
                {
                    context.src.PropertyChanged -= context.propertyChanged;
                }
            }
            bindingConexts.Clear();
        }

        public static void SetBinding(INotifyPropertyChanged src, string srcPropertyName, INotifyPropertyChanged target, string targetPropertyName, BindingMode mode)
潘栩锋's avatar
潘栩锋 committed
182 183 184 185 186 187 188
        {
            Type type_s = src.GetType();
            Type type_t = target.GetType();
            System.Reflection.PropertyInfo pi_s = type_s.GetProperty(srcPropertyName);
            System.Reflection.PropertyInfo pi_t = type_t.GetProperty(targetPropertyName);
            if (pi_s == null || pi_t == null)
                return;
189 190

            switch (mode)
潘栩锋's avatar
潘栩锋 committed
191 192 193
            {
                case BindingMode.OneWay://src->target
                    {
194
                        object obj = pi_s.GetValue(src, null);
潘栩锋's avatar
潘栩锋 committed
195 196
                        pi_t.SetValue(target, obj, null);

197 198
                        PropertyChangedEventHandler pcevent;
                        src.PropertyChanged += pcevent = (s, e) =>
潘栩锋's avatar
潘栩锋 committed
199 200 201 202 203 204 205
                        {
                            if (e.PropertyName == srcPropertyName)
                            {
                                object o = pi_s.GetValue(src, null);
                                pi_t.SetValue(target, o, null);
                            }
                        };
206 207 208 209 210 211 212 213 214 215 216 217

                        if (IsMarkdownEvents)
                        {
                            if (!bindingConexts.ContainsKey(target))
                            {
                                bindingConexts.Add(target, new List<PropertyChangedEventContexts>());
                            }
                            var list = bindingConexts[target];
                            list.Add(new PropertyChangedEventContexts() { src = src, propertyChanged = pcevent });
                        }
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
218 219
                case BindingMode.TwoWay://src->target then target->src
                    {
220
                        object obj = pi_s.GetValue(src, null);
潘栩锋's avatar
潘栩锋 committed
221 222
                        pi_t.SetValue(target, obj, null);

223 224
                        PropertyChangedEventHandler pcevent;
                        src.PropertyChanged += pcevent = (s, e) =>
潘栩锋's avatar
潘栩锋 committed
225 226 227 228 229 230 231 232
                        {
                            if (e.PropertyName == srcPropertyName)
                            {
                                object o = pi_s.GetValue(s, null);
                                pi_t.SetValue(target, o, null);
                            }
                        };

233 234 235 236 237 238 239 240 241 242 243
                        if (IsMarkdownEvents)
                        {
                            if (!bindingConexts.ContainsKey(target))
                            {
                                bindingConexts.Add(target, new List<PropertyChangedEventContexts>());
                            }
                            var list = bindingConexts[target];
                            list.Add(new PropertyChangedEventContexts() { src = src, propertyChanged = pcevent });
                        }

                        target.PropertyChanged += pcevent = (s, e) =>
潘栩锋's avatar
潘栩锋 committed
244 245 246 247 248 249 250
                        {
                            if (e.PropertyName == targetPropertyName)
                            {
                                object o = pi_t.GetValue(s, null);
                                pi_s.SetValue(src, o, null);
                            }
                        };
251 252 253 254 255 256 257 258 259 260 261 262

                        if (IsMarkdownEvents)
                        {
                            if (!bindingConexts.ContainsKey(src))
                            {
                                bindingConexts.Add(src, new List<PropertyChangedEventContexts>());
                            }
                            var list = bindingConexts[src];
                            list.Add(new PropertyChangedEventContexts() { src = target, propertyChanged = pcevent });
                        }
                    }
                    break;
潘栩锋's avatar
潘栩锋 committed
263 264
            }
        }
潘栩锋's avatar
潘栩锋 committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278

        public static void SetBinding(INotifyPropertyChanged src, string srcPropertyName, ParamDictionary target, string targetPropertyName, BindingMode mode)
        {
            Type type_s = src.GetType();
            System.Reflection.PropertyInfo pi_s = type_s.GetProperty(srcPropertyName);
            if (pi_s == null)
                return;

            switch (mode)
            {
                case BindingMode.OneWay://src->target
                    {
                        object obj = pi_s.GetValue(src, null);
                        target.SetValue(targetPropertyName, obj);
279

潘栩锋's avatar
潘栩锋 committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
                        src.PropertyChanged += (s, e) =>
                        {
                            if (e.PropertyName == srcPropertyName)
                            {
                                object o = pi_s.GetValue(src, null);
                                target.SetValue(targetPropertyName, obj);
                            }
                        };
                    }
                    break;
                case BindingMode.TwoWay://src->target then target->src
                    {
                        object obj = pi_s.GetValue(src, null);
                        target.SetValue(targetPropertyName, obj);

                        src.PropertyChanged += (s, e) =>
                        {
                            if (e.PropertyName == srcPropertyName)
                            {
                                object o = pi_s.GetValue(s, null);
                                target.SetValue(targetPropertyName, obj);
                            }
                        };

                        target.ValueChanged += (s, e) =>
                        {
                            if (e.Key == targetPropertyName)
                            {
                                object o = target.GetValue(pi_s.PropertyType, targetPropertyName);
                                pi_s.SetValue(src, o, null);
                            }
                        };
                    }
                    break;
            }
        }
潘栩锋's avatar
潘栩锋 committed
316 317 318
    }

}