2011-07-27 27 views
6

不太清楚爲什麼發生這種情況,但我希望能夠修改XNA顏色值:的WinForms屬性網格不會讓我改變結構值

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

請更新您的代碼,使用您的CustomConverter替換[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))] – Antonio

回答

3

ExpandableConverter只會顯示一個彩色的內部屬性。你將無法編輯R,G,B和A,因爲他們只有訪問者。使用ColorConverter不會顯示這些屬性,所以這不是一個解決方案。你將需要編寫你自己的轉換器。例如,使用Reflector並查看FontConverter。您將看到如何使用CreateInstance從其屬性構建新的Color。