Inheritance: BindingBase
        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;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MonoMobile.Views.BindingExpression"/> class.
        /// </summary>
        /// <param name='binding'>
        /// Binding.
        /// </param>
        /// <param name='targetProperty'>
        /// ViewModel.<targetProperty>.
        /// </param>
        /// <param name='target'>
        /// ViewModel to hock into.
        /// </param>
        /// <exception cref='ArgumentNullException'>
        /// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> .
        /// </exception>
        public BindingExpression(Binding binding, MemberInfo targetProperty, object target)
        {
            if (binding == null)
                throw new ArgumentNullException("binding");

            if (targetProperty == null)
                throw new ArgumentNullException("targetProperty");

            if (target == null)
                throw new ArgumentNullException("target");

            Binding = binding;
            Binding.Target = target;
            TargetProperty = targetProperty;
            if(string.IsNullOrEmpty(binding.TargetPath))
            {
                binding.TargetPath = targetProperty.Name;
            }

            object viewSource = Binding.Source;
            _ViewProperty = viewSource.GetType().GetNestedMember(ref viewSource, Binding.SourcePath, true);
            Binding.ViewSource = viewSource;
            SourceProperty = _ViewProperty;

            var dataContext = viewSource as IDataContext;
            if (dataContext != null && dataContext.DataContext != null)
            {
                var source = dataContext.DataContext;

                SourceProperty = source.GetType().GetNestedMember(ref source, Binding.SourcePath, true);
                Binding.Source = source;
            }
        }
		public static IBindingExpression GetBindingExpression(Binding binding)
		{
			UpdateBindings();

			if (_BindingExpressions != null)
			{
				return _BindingExpressions.SingleOrDefault((b)=>b.Binding == binding);
			}

			return null;
		}
        /// <summary>
        /// Add a single binding for any page to the list of cached bindings.
        /// </summary>
        /// <param name="viewModel"></param>
        /// <param name="elementId"></param>
        /// <param name="property"></param>
        /// <param name="bindingInfo"></param>
        public void Add(and.View view , Binding binding, IElement element )
        {
            IList<Binding> bindings;
            if(!_perPageBindings.TryGetValue(view, out bindings))
            {
                bindings = new List<Binding>();
                _perPageBindings[view] = bindings;
            }

            bindings.Add(binding);

            binding.Target = _viewModel;
            binding.Source = view;

            var expression = _bindingFactory.Create((ViewModel) binding.Target,
                                                    binding.SourcePath,
                                                    binding.TargetPath,
                                                    binding);
            expression.Element = element;

            _bindingsForCurrentPage.Add(expression);
        }
		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;
		}
		public static IBindingExpression SetBinding(IBindable target, string targetProperty, object dataContext)
		{
			Binding binding = dataContext as Binding;
			if (dataContext == null)
			{
				binding = new Binding(targetProperty, "DataContext") { Source = dataContext } ;
			}
			return SetBinding(target, binding);
		}
Beispiel #7
0
		public Element(string caption, Binding binding): this(caption)
		{

		}
		public BindAttribute(string sourcePath, string targetPath)
		{
			Binding = new Binding(sourcePath, targetPath);
		}
		public BindAttribute(string targetPath)
		{
			Binding = new Binding(targetPath);
		}
		public BindAttribute()
		{
			Binding = new Binding(null, "DataContext");
		}