Inheritance: MarkupExpressionParser
示例#1
0
		private static unsafe object GetObjectValue (object target, IntPtr target_data, string prop_name, IntPtr parser, Value* value_ptr, out string error)
		{
			error = null;

			IntPtr unmanaged_value = IntPtr.Zero;
			object o_value = Value.ToObject (null, value_ptr);
			if (error == null && unmanaged_value != IntPtr.Zero)
				o_value = Value.ToObject (null, unmanaged_value);

			if (o_value is String && SL3MarkupExpressionParser.IsStaticResource ((string) o_value)) {
				MarkupExpressionParser mp = new SL3MarkupExpressionParser ((DependencyObject) target, prop_name, parser, target_data);
				string str_value = o_value as String;
				o_value = mp.ParseExpression (ref str_value);
			}

			return o_value;
		}
示例#2
0
		private unsafe bool TrySetExpression (XamlCallbackData *data, string xmlns, object target, IntPtr target_data, Value* target_parent_ptr, string type_name, string prop_xmlns, string name, string full_name, Value* value_ptr, IntPtr value_data)
		{
			DependencyObject dob = target as DependencyObject;
			object obj_value = Value.ToObject (null, value_ptr);
			string str_value = obj_value as string;

			if (str_value == null)
				return false;
			
			if (!str_value.StartsWith ("{"))
				return false;

			MarkupExpressionParser p = new SL3MarkupExpressionParser (target, name, data->parser, target_data);
			string expression = str_value;
			object o = p.ParseExpression (ref expression);

			if (o == null)
				return false;

			
			if (o is Binding) {
				Binding binding = o as Binding;
				DependencyProperty prop = null;

				if (dob != null) {
					string full_type_name = type_name;
					if (IsAttachedProperty (full_name))
						GetNameForAttachedProperty (xmlns, prop_xmlns, full_name, out type_name, out full_type_name);
					prop = LookupDependencyPropertyForBinding (data, dob, full_type_name, name);
					if (prop == null && IsAttachedProperty (full_name))
						prop = LookupDependencyPropertyForBinding (data, dob, full_name.Split ('.')[0], name);
				}

				// If it's null we should look for a regular CLR property
				if (prop != null) {
					BindingOperations.SetBinding (dob, prop, binding);
					return true;
				}
			}
			if (o is TemplateBindingExpression) {
				// Applying a {TemplateBinding} to a DO which is not a FrameworkElement should silently discard
				// the binding.
				if (!(dob is FrameworkElement))
					return true;

				TemplateBindingExpression tb = o as TemplateBindingExpression;

				IntPtr context = NativeMethods.sl3_xaml_loader_get_context (data->loader);
				IntPtr source_ptr = NativeMethods.xaml_context_get_template_binding_source (context);

				// Silently discard TemplateBindings which are not in ControlTemplates
				FrameworkTemplate source_template = (FrameworkTemplate) NativeDependencyObjectHelper.Lookup (NativeMethods.xaml_context_get_source_template (context));
				if (!(source_template is ControlTemplate))
					return true;

				DependencyObject templateSourceObject = NativeDependencyObjectHelper.FromIntPtr (source_ptr) as DependencyObject;
				if (templateSourceObject == null)
					return false;

				DependencyProperty sourceProperty = DependencyProperty.Lookup (templateSourceObject.GetKind(),
												       tb.SourcePropertyName);
				if (sourceProperty == null)
					return false;

				DependencyProperty prop = null;

				if (dob != null)
					prop = LookupDependencyPropertyForBinding (data, dob, type_name, name);

				if (prop == null)
					return false;

				tb.TargetProperty = prop;
				tb.SourceProperty = sourceProperty;

				((FrameworkElement) dob).SetTemplateBinding (prop, tb);

				return true;
			}

			if (IsAttachedProperty (full_name))
				return TrySetAttachedProperty (data, xmlns, target, target_data, prop_xmlns, full_name, o);

			PropertyInfo pi = target.GetType ().GetProperty (name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy);

			o = ConvertType (pi, pi.PropertyType, o);
			SetValue (data, target_data, pi, target, o);
			return true;
		}
示例#3
0
		private unsafe bool SetPropertyFromValue (XamlCallbackData *data, object target, IntPtr target_data, Value* target_parent_ptr, PropertyInfo pi, Value* value_ptr, IntPtr value_data, out string error)
		{
			error = null;
			object obj_value = Value.ToObject (null, value_ptr);
			
			if (pi.GetCustomAttributes (typeof (SetPropertyDelayedAttribute), true).Length > 0) {
				if ((data->flags & XamlCallbackFlags.SettingDelayedProperty) == 0) {
					Value v = *value_ptr;
					NativeMethods.xaml_delay_set_property (data->parser, target_data, null, pi.Name, ref v);
					return true;
				}
			}

			if (obj_value is Binding && target is FrameworkElement) {
				FrameworkElement fe = (FrameworkElement) target;
				fe.SetBinding (DependencyProperty.Lookup (fe.GetKind (), pi.Name), (Binding) obj_value);
				return true;
			}

			if (obj_value is StaticResource) {
				StaticResource sr = (StaticResource)obj_value;
				obj_value = "{StaticResource " + sr.ResourceKey + "}";
			}

			if (typeof (IList).IsAssignableFrom (pi.PropertyType) && !(obj_value is IList)) {
				// This case is handled in the AddChild code
				return true;
			}

			if (typeof (ResourceDictionary).IsAssignableFrom (pi.PropertyType) && !(obj_value is ResourceDictionary)) {
				// This case is handled in the AddChild code
				return true;
			}

			string str_value = obj_value as string;
			if (str_value != null) {
				IntPtr unmanaged_value;
				
				if (pi.PropertyType == typeof (Type)) {
					Type t = TypeFromString (data, str_value);
					if (t != null) {
						SetValue (data, target_data, pi, target, t);
						return true;
					}
				}

				if (pi.PropertyType == typeof (DependencyProperty)) {
					DependencyProperty dp = DependencyPropertyFromString (data, target, target_parent_ptr, str_value);
					if (dp != null) {
						SetValue (data, target_data, pi, target, dp);
						return true;
					}
				}

				if (typeof (System.Windows.Data.Binding).IsAssignableFrom (pi.PropertyType) && SL3MarkupExpressionParser.IsBinding (str_value)) {
					MarkupExpressionParser p = new SL3MarkupExpressionParser (null, pi.Name,  data->parser, target_data);

					string expression = str_value;
					obj_value = p.ParseExpression (ref expression);

					if (!(obj_value is Binding))
						return false;

					SetValue (data, target_data, pi, target, obj_value);
					return true;
				}

				if (SL3MarkupExpressionParser.IsStaticResource (str_value)) {
					MarkupExpressionParser p = new SL3MarkupExpressionParser (null, "", data->parser, target_data);
					obj_value = p.ParseExpression (ref str_value);

					obj_value = ConvertType (pi, pi.PropertyType, obj_value);

					SetValue (data, target_data, pi, target, obj_value);
					return true;
				}

				SetCLRPropertyFromString (data, target_data, target, pi, str_value, out error, out unmanaged_value);

				try {
					if (error == null && unmanaged_value != IntPtr.Zero)
						obj_value = Value.ToObject (null, unmanaged_value);
					else
						return error == null;
				} finally {
					if (unmanaged_value != IntPtr.Zero)
						Mono.NativeMethods.value_delete_value2 (unmanaged_value);
				}
			} else {
				obj_value = Value.ToObject (pi.PropertyType, value_ptr);
			}

			obj_value = ConvertType (pi, pi.PropertyType, obj_value);
			SetValue (data, target_data, pi, target, obj_value);

			return true;
		}