public Thickness(Thickness other) { _left = other._left; _top = other._top; _right = other._right; _bottom = other._bottom; }
public static bool ConvertType(object value, Type targetType, out object result) { result = value; if (value == null) return true; if (value is string && targetType == typeof(Type)) { string typeName = (string)value; Type type; if (!_objectClassRegistrations.TryGetValue(typeName, out type)) type = Type.GetType(typeName); if (type != null) { result = type; return true; } } // Don't convert LateBoundValue (or superclass ValueWrapper) here... instances of // LateBoundValue must stay unchanged until some code part explicitly converts them! if (value is ResourceWrapper) { object resource = ((ResourceWrapper)value).Resource; if (TypeConverter.Convert(resource, targetType, out result)) { if (ReferenceEquals(resource, result)) { // Resource must be copied because setters and other controls most probably need a copy of the resource. // If we don't copy it, Setter is not able to check if we already return a copy because our input value differs // from the output value, even if we didn't do a copy here. result = MpfCopyManager.DeepCopyCutLVPs(result); } return true; } } if (value is string && targetType == typeof(FrameworkElement)) { // It doesn't suffice to have an implicit data template declaration which returns a label for a string. // If you try to build a ResourceWrapper with a string and assign that ResourceWrapper to a Button's Content property // with a StaticResource, for example, the ResourceWrapper will be assigned directly without the data template being // applied. To make it sill work, we need this explicit type conversion here. result = new Label { Content = (string)value, Color = Color.White }; return true; } if (targetType == typeof(Transform)) { string v = value.ToString(); string[] parts = v.Split(new[] { ',' }); if (parts.Length == 6) { float[] f = new float[parts.Length]; for (int i = 0; i < parts.Length; ++i) { object obj; TypeConverter.Convert(parts[i], typeof(double), out obj); f[i] = (float)obj; } System.Drawing.Drawing2D.Matrix matrix2D = new System.Drawing.Drawing2D.Matrix(f[0], f[1], f[2], f[3], f[4], f[5]); Static2dMatrix matrix = new Static2dMatrix(); matrix.Set2DMatrix(matrix2D); result = matrix; return true; } } else if (targetType == typeof(Vector2)) { result = Convert2Vector2(value.ToString()); return true; } else if (targetType == typeof(Vector3)) { result = Convert2Vector3(value.ToString()); return true; } else if (targetType == typeof(Vector4)) { result = Convert2Vector4(value.ToString()); return true; } else if (targetType == typeof(Thickness)) { Thickness t; float[] numberList = ParseFloatList(value.ToString()); if (numberList.Length == 1) { t = new Thickness(numberList[0]); } else if (numberList.Length == 2) { t = new Thickness(numberList[0], numberList[1]); } else if (numberList.Length == 4) { t = new Thickness(numberList[0], numberList[1], numberList[2], numberList[3]); } else { throw new ArgumentException("Invalid # of parameters"); } result = t; return true; } else if (targetType == typeof(Color)) { Color color; if (ColorConverter.ConvertColor(value, out color)) { result = color; return true; } } else if (targetType == typeof(Brush) && value is string || value is Color) { try { Color color; if (ColorConverter.ConvertColor(value, out color)) { SolidColorBrush b = new SolidColorBrush { Color = color }; result = b; return true; } } catch (Exception) { return false; } } else if (targetType == typeof(GridLength)) { string text = value.ToString(); if (text == "Auto") result = new GridLength(GridUnitType.Auto, 0.0); else if (text == "AutoStretch") result = new GridLength(GridUnitType.AutoStretch, 1.0); else if (text.IndexOf('*') >= 0) { int pos = text.IndexOf('*'); text = text.Substring(0, pos); if (text.Length > 0) { object obj; TypeConverter.Convert(text, typeof(double), out obj); result = new GridLength(GridUnitType.Star, (double)obj); } else result = new GridLength(GridUnitType.Star, 1.0); } else { double v = double.Parse(text); result = new GridLength(GridUnitType.Pixel, v); } return true; } else if (targetType == typeof(string) && value is IResourceString) { result = ((IResourceString)value).Evaluate(); return true; } else if (targetType.IsAssignableFrom(typeof(IExecutableCommand)) && value is ICommand) { result = new CommandBridge((ICommand)value); return true; } else if (targetType == typeof(Key) && value is string) { string str = (string)value; // Try a special key result = Key.GetSpecialKeyByName(str); if (result == null) if (str.Length != 1) throw new ArgumentException(string.Format("Cannot convert '{0}' to type Key", str)); else result = new Key(str[0]); return true; } else if (targetType == typeof(string) && value is IEnumerable) { result = StringUtils.Join(", ", (IEnumerable)value); return true; } result = value; return false; }
/// <summary> /// Removes the given <paramref name="margin"/> from the specified <paramref name="rect"/>. /// </summary> /// <param name="rect">Outer element's rectangle where the margin will be removed.</param> /// <param name="margin">Margin to be removed.</param> public static void RemoveMargin(ref RectangleF rect, Thickness margin) { rect.X += margin.Left; rect.Y += margin.Top; rect.Width -= margin.Left + margin.Right; rect.Height -= margin.Top + margin.Bottom; }
/// <summary> /// Removes the given <paramref name="margin"/> from the specified <param name="size"/> parameter. /// </summary> /// <remarks> /// <see cref="float.NaN"/> values will be preserved, i.e. if a <paramref name="size"/> coordinate /// is <see cref="float.NaN"/>, it won't be changed. /// </remarks> /// <param name="size">Size parameter where the margin will be removed.</param> /// <param name="margin">Margin to be removed.</param> public static void RemoveMargin(ref SizeF size, Thickness margin) { if (!float.IsNaN(size.Width)) size.Width -= margin.Left + margin.Right; if (!float.IsNaN(size.Height)) size.Height -= margin.Top + margin.Bottom; }
/// <summary> /// Adds the given <paramref name="margin"/> to the specified <paramref name="rect"/>. /// </summary> /// <param name="rect">Inner element's rectangle where the margin will be added.</param> /// <param name="margin">Margin to be added.</param> public static void AddMargin(ref RectangleF rect, Thickness margin) { rect.X -= margin.Left; rect.Y -= margin.Top; rect.Width += margin.Left + margin.Right; rect.Height += margin.Top + margin.Bottom; }
public override void DeepCopy(IDeepCopyable source, ICopyManager copyManager) { Detach(); base.DeepCopy(source, copyManager); UIElement el = (UIElement) source; // We do not copy the focus flag, only one element can have focus //HasFocus = el.HasFocus; string copyName = el.Name; ActualPosition = el.ActualPosition; Margin = new Thickness(el.Margin); Visibility = el.Visibility; IsEnabled = el.IsEnabled; // TODO Albert78: Implement Freezing Freezable = el.Freezable; Opacity = el.Opacity; OpacityMask = copyManager.GetCopy(el.OpacityMask); LayoutTransform = copyManager.GetCopy(el.LayoutTransform); RenderTransform = copyManager.GetCopy(el.RenderTransform); RenderTransformOrigin = copyManager.GetCopy(el.RenderTransformOrigin); Effect = copyManager.GetCopy(el.Effect); TemplateNameScope = copyManager.GetCopy(el.TemplateNameScope); _resources = copyManager.GetCopy(el._resources); foreach (TriggerBase t in el.Triggers) Triggers.Add(copyManager.GetCopy(t)); _triggersInitialized = false; copyManager.CopyCompleted += (cm => { // When copying, the namescopes of our parent objects might not have been initialized yet. This can be the case // when the TemplateNamescope property or a LogicalParent property wasn't copied yet, for example. // That's why we cannot simply copy the Name property in the DeepCopy method. Name = copyName; copyName = null; }); Attach(); }