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; }
public void SetDesiredLength(int cellIndex, int cellSpan, double desiredLength) { // Not set, don't bother. if (double.IsNaN(desiredLength)) { return; } if (cellIndex < 0 || cellIndex >= Count) { ServiceRegistration.Get <ILogger>().Warn("{0}: Invalid cell index {1}; valid range is {2}-{3}", GetType().Name, cellIndex, 0, Count - 1); if (cellIndex < 0) { cellIndex = 0; } else { cellIndex = Count - 1; } } if (cellSpan < 0 || cellSpan + cellIndex > Count) { ServiceRegistration.Get <ILogger>().Warn("{0}: Invalid cell span {1} in cell {2}; valid range is {3}-{4}", GetType().Name, cellSpan, cellIndex, 1, Count - cellIndex); if (cellSpan < 0) { cellSpan = 0; } else { cellSpan = Count - cellIndex; } } int relativeCount = 0; for (int i = 0; i < cellSpan; i++) { GridLength length = this[i + cellIndex].Length; if (length.IsAbsolute) { desiredLength -= length.DesiredLength; } else { relativeCount++; } } bool starAvailable = false; // Determine if we have a Star column which will get the size primarily for (int i = 0; i < cellSpan; i++) { GridLength length = this[i + cellIndex].Length; starAvailable |= length.IsAutoStretch || length.IsStar; } for (int i = 0; i < cellSpan; i++) { GridLength length = this[i + cellIndex].Length; if (length.IsAutoStretch || length.IsStar || (length.IsAuto && !starAvailable)) { if (length.DesiredLength < desiredLength / relativeCount) { length.DesiredLength = desiredLength / relativeCount; } } } }