2011-01-14 38 views

Trả lời

7

Không có cách nào dễ dàng.

Bạn có thể làm việc này bằng cách triển khai ICustomTypeDescriptor. Đây là một bài viết hay về implementing ICustomTypeDescriptor.

Hoặc bạn có thể liên kết ControlDesigner của riêng mình với lớp học của bạn và ghi đè phương thức PreFilterProperties để thêm hoặc xóa thuộc tính được xem trong lưới thuộc tính.

Removing certain properties from property grid.

+2

liên kết để triển khai ICustomTypeDescriptor.got được chỉ tới https://msdn.microsoft.com/magazine/msdn-magazine-issues Tôi nghĩ rằng họ đã xóa trang gốc – gg89

6

Bạn có thể thực hiện việc này bằng cách cung cấp mô hình kiểu tùy chỉnh; ở mức đơn giản nhất, bạn có thể cung cấp TypeDescriptor tùy chỉnh cho loại của mình bắt nguồn từ ExpandableObjectConverter và chỉ cần bao gồm/loại trừ thuộc tính đã cho lúc này - nhưng điều này chỉ hoạt động với PropertyGrid - được sử dụng bởi trang thuộc tính. Một cách tiếp cận phức tạp hơn là sử dụng ICustomTypeDescriptor/TypeDescriptionProvider - điều này có thể hoạt động bên trong những thứ như DataGridView

0

tôi tình cờ này để tìm kiếm một cách để khai báo một số thành viên có thể nhìn thấy hoặc ẩn trong IntelliSense và có thể thay đổi nó một lần cho tất cả những gì cần thiết để được ẩn lúc biên dịch. Tôi không thể biết đó có phải là điều bạn đang tìm kiếm hay không, nhưng tôi đã tìm thấy câu trả lời cho câu hỏi của tôi ... tôi không thể chia sẻ.

tôi đặt một biểu tượng có điều kiện biên soạn (tìm thấy trong tab Build tài sản dự án) IS_VIS (giá trị là đúng nếu bạn muốn các thành viên nhất định để hiển thị, false nếu muốn bạn để ẩn chúng) và sau đó:

#if IS_VIS 
    public const System.ComponentModel.EditorBrowsableState isVis = 
     ComponentModel.EditorBrowsableState.Always; 
#else 
    public const System.ComponentModel.EditorBrowsableState isVis = 
     ComponentModel.EditorBrowsableState.Never; 
#endif 

bạn sau đó tham chiếu biến isVis trong thuộc tính:

[EditorBrowsable(isVis)] 
public string myMethod... 

Tôi đã làm điều này trong VB và điều này đã nhanh chóng được chuyển thành C#. Nếu một cái gì đó không hoạt động đúng, hãy cho tôi biết.

8

Tôi không chắc chắn điều này áp dụng cho trường hợp của bạn, nhưng bạn có thể điều chỉnh trang trí "Có thể duyệt" trong thời gian chạy bằng cách gọi hàm bên dưới.

/// <summary> 
/// Set the Browsable property. 
/// NOTE: Be sure to decorate the property with [Browsable(true)] 
/// </summary> 
/// <param name="PropertyName">Name of the variable</param> 
/// <param name="bIsBrowsable">Browsable Value</param> 
private void setBrowsableProperty(string strPropertyName, bool bIsBrowsable) 
{ 
    // Get the Descriptor's Properties 
    PropertyDescriptor theDescriptor = TypeDescriptor.GetProperties(this.GetType())[strPropertyName]; 

    // Get the Descriptor's "Browsable" Attribute 
    BrowsableAttribute theDescriptorBrowsableAttribute = (BrowsableAttribute)theDescriptor.Attributes[typeof(BrowsableAttribute)]; 
    FieldInfo isBrowsable = theDescriptorBrowsableAttribute.GetType().GetField("Browsable", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance); 

    // Set the Descriptor's "Browsable" Attribute 
    isBrowsable.SetValue(theDescriptorBrowsableAttribute, bIsBrowsable); 
} 
+0

Xin chào. Tôi đang tìm một thứ như thế này. Khi tôi sử dụng mã của bạn, tôi nhận được một ngoại lệ nullValue trong dòng đầu tiên 'PropertyDescriptor theDescriptor = TypeDescriptor.GetProperties (this.GetType()) [strPropertyName];'.Tôi muốn biết nơi để đặt chức năng này và làm thế nào nó phản ánh trên lưới bất động sản (không nhìn thấy một đối tượng propertyGrid trong chức năng). –

+1

Cảm ơn bạn rất nhiều vì giải pháp của bạn, tôi đã thay đổi dòng thành 'PropertyDescriptor theDescriptor = TypeDescriptor.GetProperties (vObject.GetType()) [strPropertyName‌];', trong đó vObject là đối tượng tôi đang thay đổi. –

0

Như một sự cải tiến về câu trả lời @ neoikon của trên và để tránh sự ngoại lệ Ganesh đề cập trong các ý kiến, đây là một phiên bản sử dụng Generics để có được các loại:

/// <summary> 
    /// Set the Browsable property. 
    /// NOTE: Be sure to decorate the property with [Browsable(true)] 
    /// </summary> 
    /// <param name="PropertyName">Name of the variable</param> 
    /// <param name="bIsBrowsable">Browsable Value</param> 
    private void SetBrowsableProperty<T>(string strPropertyName, bool bIsBrowsable) 
    { 
     // Get the Descriptor's Properties 
     PropertyDescriptor theDescriptor = TypeDescriptor.GetProperties(typeof(T))[strPropertyName]; 

     // Get the Descriptor's "Browsable" Attribute 
     BrowsableAttribute theDescriptorBrowsableAttribute = (BrowsableAttribute)theDescriptor.Attributes[typeof(BrowsableAttribute)]; 
     FieldInfo isBrowsable = theDescriptorBrowsableAttribute.GetType().GetField("Browsable", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance); 

     // Set the Descriptor's "Browsable" Attribute 
     isBrowsable.SetValue(theDescriptorBrowsableAttribute, bIsBrowsable); 
    } 

Sau đó, bạn cũng có thể thêm một phiên bản mà phải mất một ví dụ:

/// <summary> 
    /// Set the Browsable property. 
    /// NOTE: Be sure to decorate the property with [Browsable(true)] 
    /// </summary> 
    /// <param name="obj">An instance of the object whose property should be modified.</param> 
    /// <param name="PropertyName">Name of the variable</param> 
    /// <param name="bIsBrowsable">Browsable Value</param> 
    private void SetBrowsableProperty<T>(T obj, string strPropertyName, bool bIsBrowsable) 
    { 
     SetBrowsableProperty<T>(strPropertyName, bIsBrowsable); 
    } 

Cách sử dụng:

class Foo 
    { 
     [Browsable(false)] 
     public string Bar { get; set; } 
    } 
    void Example() 
    { 
     SetBrowsableProperty<Foo>("Bar", true); 
     Foo foo = new Foo(); 
     SetBrowsableProperty(foo, "Bar", false); 
    } 
Các vấn đề liên quan