2013-04-24 30 views
5

Khi sử dụng lưới thuộc tính, làm cách nào để cho phép tôi đặt một thuộc tính cho nhiều đối tượng cùng một lúc (khi có cấp độ thứ hai). Mọi thứ dường như hoạt động tốt nếu cả hai thuộc tính có cùng giá trị, nhưng nếu các thuộc tính không bằng propertygrid thì không tải các thuộc tính con (từ mũi tên sang phải) để chúng không thể được thiết lập. Tôi đã tạo ví dụ sau, xin lỗi về độ dài của mã.Cách lấy lưới thuộc tính để cho phép chỉnh sửa đối tượng cho đa lựa chọn

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     this.Size = new Size(275, 568); 
     PropertyGrid grid = new PropertyGrid(); 
     grid.Dock = DockStyle.Fill; 
     this.Controls.Add(grid); 


     Control c1 = new Control(); 
     c1.Border.Bottom.Color = Color.Red; 
     Control c2 = new Control(); 
     c2.Border.Bottom.Color = Color.Red; 
     Control c3 = new Control(); 
     c3.Border.Bottom.Color = Color.Purple; 

     //This works as expected 
     //grid.SelectedObject = c1; 

     //This works as expected 
     //grid.SelectedObjects = new object[] { c1, c2 }; 

     //This does not work 
     grid.SelectedObjects = new object[] { c1, c3 }; 

    } 
} 

class Control 
{ 
    public Control() 
    { 
     Border = new Border(); 
     Text = "Text"; 
    } 

    public string Text { get; set; } 
    [TypeConverter(typeof(BorderConverter))] 
    public Border Border { get; set; } 
} 

class Border 
{ 
    public Border() 
    { 
     Top = new Line(); 
     Bottom = new Line(); 
     Right = new Line(); 
     Left = new Line(); 
    } 
    [TypeConverter(typeof(LineConverter))] 
    public Line Top { get; set; } 
    [TypeConverter(typeof(LineConverter))] 
    public Line Left { get; set; } 
    [TypeConverter(typeof(LineConverter))] 
    public Line Bottom { get; set; } 
    [TypeConverter(typeof(LineConverter))] 
    public Line Right { get; set; } 

    public override bool Equals(object obj) 
    { 
     if ((obj as Border) == null) 
      return false; 
     Border b = (Border)obj; 
     return b.Left.Equals(Left) && 
      b.Right.Equals(Right) && b.Top.Equals(Top) 
      && b.Bottom.Equals(Bottom); 
    } 

    public override int GetHashCode() 
    { 
     return base.GetHashCode(); 
    } 
} 

public class BorderConverter 
: TypeConverter 
{ 
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
    { 
     return base.CanConvertFrom(context, sourceType); 
    } 

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType)); 
    } 

    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) 
    { 
     return attributes == null ? TypeDescriptor.GetProperties(value) : TypeDescriptor.GetProperties(value, attributes); 
    } 

    public override bool GetPropertiesSupported(ITypeDescriptorContext context) 
    { 

     return true; 
    } 
} 

class Line 
{ 
    public Line() 
    { 
     Color = Color.Black; 
     Wieght = 0; 
    } 
    public Color Color { get; set; } 
    public int Wieght { get; set; } 

    public override bool Equals(object obj) 
    { 
     if ((obj as Line) == null) 
      return false; 
     Line l = (Line)obj; 
     return l.Color == Color && l.Wieght == Wieght; 
    } 

    public override int GetHashCode() 
    { 
     return base.GetHashCode(); 
    } 
} 

public class LineConverter 
: TypeConverter 
{ 
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
    { 
     return base.CanConvertFrom(context, sourceType); 
    } 

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType)); 
    } 

    public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues) 
    { 
     if (propertyValues == null) throw new ArgumentNullException("propertyValues"); 
     try 
     { 
      Line bs = new Line(); 
      bs.Color = (Color)propertyValues["Color"]; 
      bs.Wieght = (int)propertyValues["Wieght"]; 
      return bs; 
     } 
     catch (Exception ex) { throw new Exception("Invalid Property value.", ex); } 
    } 

    public override bool GetCreateInstanceSupported(ITypeDescriptorContext context) 
    { 
     return true; 
    } 

    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) 
    { 
     return attributes == null ? TypeDescriptor.GetProperties(value) : TypeDescriptor.GetProperties(value, attributes); 
    } 

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context) 
    { 
     return base.GetStandardValuesSupported(context); 
    } 

    public override bool GetPropertiesSupported(ITypeDescriptorContext context) 
    { 

     return true; 
    } 
} 

Trả lời

2

tôi quản lý để có được nó hoạt động bằng cách loại bỏ GetCreateInstanceSupported phương pháp từ LineConverter, nhưng tôi không biết nguyên nhân chính của vấn đề của bạn là chính xác những gì kể từ khi tôi không có kiến ​​thức sâu rộng như thế nào PropertyGrid đang sử dụng TypeDescriptor cơ sở hạ tầng.

+0

không chính xác những gì tôi đang tìm kiếm, nhưng đó là câu trả lời! –

Các vấn đề liên quan