public override void DeepCopy(IDeepCopyable source, ICopyManager copyManager)
        {
            base.DeepCopy(source, copyManager);
            CommandBridge cb = (CommandBridge)source;

            _command = copyManager.GetCopy(cb._command);
        }
示例#2
0
    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;
    }