2011-07-27 5 views
6

꽤 확실하지 왜 이런 일이 일어나고,하지만 난 XNA 색상 값을 수정할 수 있도록하려면 :윈폼 재산권 그리드 나 구조체 값을 변경하는 것을 허용하지 않습니다

private Color _color = Color.White; 

[System.ComponentModel.Category("VisibleInEditor")] 
[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))] 
public Color Color 
{ 
    get { return _color; } 
    set { _color = value; } 
} 

나는 ExpandableObjectConverter 속성을 가진 생각을 것 문제를 해결했지만 아직 완료하지 않았습니다.

편집 :

public class ColorTypeConverter : ExpandableObjectConverter 
{ 
    public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType) 
    { 
     return destinationType == typeof(Color); 
    } 

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) 
    { 
     if (destinationType == typeof(string) && value is Color) 
     { 
      Color color = (Color)value; 
      return string.Format("{0}, {1}, {2}, {3}", color.R, color.G, color.B, color.A); 
     } 
     else return base.ConvertTo(context, culture, value, destinationType); 
    } 

    public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType) 
    { 
     return sourceType == typeof(string); 
    } 

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) 
    { 
     if (value is string) 
     { 
      try 
      { 
       string strVal = value as string; 
       var parts = strVal.Split(','); 

       byte r = byte.Parse(parts[0]); 
       byte g = byte.Parse(parts[1]); 
       byte b = byte.Parse(parts[2]); 
       byte a = byte.Parse(parts[3]); 

       return new Color(r, g, b, a); 
      } 
      catch 
      { 
       throw new ArgumentException("Can not convert '" + (string)value + "'to type Color"); 
      } 
     } 
     else return base.ConvertFrom(context, culture, value); 
    } 
    public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues) 
    { 
     return new Color((byte)propertyValues["R"], (byte)propertyValues["G"], (byte)propertyValues["B"], (byte)propertyValues["A"]); 
    } 
    public override bool GetCreateInstanceSupported(ITypeDescriptorContext context) 
    { 
     return true; 
    } 
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) 
    { 
     PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value, attributes); 

     string[] sortOrder = new string[4]; 

     sortOrder[0] = "R"; 
     sortOrder[1] = "G"; 
     sortOrder[2] = "B"; 
     sortOrder[3] = "A"; 

     // Return a sorted list of properties 
     return properties.Sort(sortOrder); 
    } 

    public override bool GetPropertiesSupported(ITypeDescriptorContext context) 
    { 
     return true; 
    } 
} 
+0

코드를 업데이트하고 [System.ComponentModel.TypeConverter (typeof (System.ComponentModel.ExpandableObjectConverter))]를 CustomConverter로 바꿉니다. – Antonio

답변

3

ExpandableConverter는 색상의 내부 속성을 바로 표시됩니다 : 나는 다음과 같은 작업 코드를 함께 패치 할 수 있었다. R, G, B 및 A는 접근 자만 가지고 있기 때문에 편집 할 수 없습니다. ColorConverter를 사용하면 이러한 속성이 표시되지 않으므로 해결책이 아닙니다. 자신 만의 변환기를 작성해야합니다. Reflector를 사용하고 예를 들어 FontConverter를 살펴보십시오. CreateInstance를 사용하여 속성에서 새 Color를 작성하는 방법을 볼 수 있습니다.

관련 문제