A bindingExpression is a single instance of a binding. A binding is essentially the information to create an instance (ie a bindingExpression).
Inheritance: IBindingExpression
示例#1
0
        public BindingExpression Create(IViewModel viewModel, String elementId, String targetProperty, Binding binding)
        {
            var type = viewModel.GetType();

            // find property dic
            IDictionary<string, MemberInfo> propertyDic;
            if(!_typeBindingDic.TryGetValue(type, out propertyDic))
            {
                propertyDic = new Dictionary<string, MemberInfo>();
                _typeBindingDic[type] = propertyDic;
            }

            MemberInfo propertyInfo = null;

            if(!propertyDic.TryGetValue(targetProperty, out propertyInfo))
            {
                // find the member...
                propertyInfo = type.GetMember(targetProperty).FirstOrDefault();

                if(propertyInfo == null)
                {
                    return null;
                }
                propertyDic[targetProperty] = propertyInfo;
            }
            var bindingExpression = new BindingExpression(binding, propertyInfo, viewModel);

            return bindingExpression;
        }
		public static IBindingExpression SetBinding(IBindable target, Binding binding)
		{
			if (target == null)
				throw new ArgumentNullException("target");

			if (binding == null)
				throw new ArgumentNullException("binding");
			
			var targetProperty = binding.TargetPath;

			IBindingExpression bindingExpression = null;
			
			object dataBinding = target.DataBinding;
			object nestedTarget = dataBinding;
			var element = target as IElement;

			MemberInfo memberInfo = null;
			FieldInfo bindablePropertyInfo = null;

			if (dataBinding != null)
			{
				var name = string.Concat(targetProperty, "Property");
				bindablePropertyInfo = dataBinding.GetType().GetField(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
				
				name = string.Concat(name, ".ControlValue");			
				memberInfo = dataBinding.GetType().GetNestedMember(ref nestedTarget, name, false);
				if (memberInfo != null)
				{
					binding.TargetPath = name;
					binding.Target = nestedTarget;
				}
			}

			var targetReady = memberInfo != null && nestedTarget != null && binding.Source != null;

			if (targetReady)
			{
				if (_BindingExpressions == null)
					_BindingExpressions = new List<IBindingExpression>();
				
				bindingExpression = GetBindingExpression(binding);

				if (bindingExpression != null)
				{
					_BindingExpressions.Remove(bindingExpression);
				}
			
				bindingExpression = new BindingExpression(binding, memberInfo, nestedTarget) { Element = element };

				_BindingExpressions.Add(bindingExpression);
					
				var vmINPC = bindingExpression.Binding.Source as INotifyPropertyChanged;
				if (vmINPC != null)
				{
					vmINPC.PropertyChanged -= HandleDataContextPropertyChanged;
					vmINPC.PropertyChanged += HandleDataContextPropertyChanged;
				}

				var viewINPC = bindingExpression.Binding.ViewSource as INotifyPropertyChanged;
				if (viewINPC != null)
				{
					viewINPC.PropertyChanged -= HandleDataContextPropertyChanged;
					viewINPC.PropertyChanged += HandleDataContextPropertyChanged;
				}
				
				var sourceValue = bindingExpression.GetSourceValue();
				 
				var sourceCollection = sourceValue as INotifyCollectionChanged;
				if (sourceCollection != null)
				{	
					SetNotificationCollectionHandler(bindingExpression, sourceCollection);
				}	
			}
			else
			{
				var binderKey =_Bindings.SingleOrDefault((kvp)=>kvp.Key.Object == target && kvp.Key.Property == targetProperty).Key;

				if (binderKey == null)
					_Bindings.Add(new PropertyBinder() { Object = target, Property = targetProperty }, binding);
				else
					_Bindings[binderKey] = binding;
			}
			
			if (bindablePropertyInfo != null)
			{
				var bindableProperty = bindablePropertyInfo.GetValue(dataBinding) as BindableProperty;
				if (bindableProperty != null)
					bindableProperty.BindingExpression = bindingExpression;
			}

			return bindingExpression;
		}