2011-07-27 12 views
6

Không khá chắc chắn tại sao điều này đang xảy ra, nhưng tôi muốn để có thể sửa đổi giá trị XNA màu:WinForms tài sản lưới điện sẽ không cho phép tôi thay đổi giá trị struct

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; } 
} 

Tôi nghĩ có thuộc tính ExpandableObjectConverter sẽ khắc phục vấn đề, nhưng nó vẫn chưa thực hiện.

Edit: tôi đã có thể vá lại với nhau đoạn code làm việc sau:

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

vui lòng cập nhật mã của bạn, thay thế [System.ComponentModel.TypeConverter (typeof (System.ComponentModel.ExpandableObjectConverter))] bằng Trình chỉnh sửa tùy chỉnh của bạn – Antonio

Trả lời

3

ExpandableConverter sẽ chỉ hiển thị các thuộc tính bên trong của một màu. Bạn sẽ không thể chỉnh sửa R, G, B và A vì chúng chỉ có quyền truy cập. Sử dụng ColorConverter sẽ không hiển thị cho bạn những thuộc tính này, vì vậy đây không phải là giải pháp. Bạn sẽ cần phải viết trình chuyển đổi của riêng bạn. Sử dụng Reflector và có một cái nhìn tại FontConverter ví dụ. Bạn sẽ thấy cách CreateInstance có thể được sử dụng để xây dựng một màu mới từ các thuộc tính của nó.