Пример #1
0
        /// <summary>
        /// Sets the specified <see cref="Binding"/> instance for the object.
        /// </summary>
        /// <param name="obj">The target object.</param>
        /// <param name="binding">The <see cref="Binding"/> instance to be set.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="binding"/> is <c>null</c>.</exception>
        public static void SetBinding(this INotifyPropertyChanged obj, Binding binding)
        {
            if (binding == null)
            {
                throw new ArgumentNullException("binding");
            }

            Binding current = null;

            var list = bindingSet.GetValueOrDefault(obj);

            if (list == null)
            {
                bindingSet[obj] = new List <Binding>();
            }
            else
            {
                current = list.FirstOrDefault(b => b.Equals(binding));
            }

            if (current == null)
            {
                binding.Target = obj;
                bindingSet[binding.Target].Add(binding);
                binding.Activate();
            }
            else
            {
                current.Mode                    = binding.Mode;
                current.ValueConverter          = binding.ValueConverter;
                current.ValueConverterParameter = binding.ValueConverterParameter;
                current.Target                  = obj;
                current.Source                  = binding.Source;
            }
        }