.net - 在设计 View 属性网格中禁用属性

标签 .net winforms user-controls propertygrid design-view

我想公开自定义控件中的一些属性。我需要从控件中获取三个参数的输入,这些参数公开为 Browsable 属性。根据一个属性的输入,可能不需要其他两个属性。如何根据第一个属性的选择禁用/隐藏不需要的属性?

最佳答案

是的,只要稍加思考,您就可以实现这一目标:

public class TestControl : Control {
  private string _PropertyA = string.Empty;
  private string _PropertyB = string.Empty;

  [RefreshProperties(RefreshProperties.All)]
  public string PropertyA {
    get { return _PropertyA; }
    set {
      _PropertyA = value;

      PropertyDescriptor pd = TypeDescriptor.GetProperties(this.GetType())["PropertyB"];
      ReadOnlyAttribute ra = (ReadOnlyAttribute)pd.Attributes[typeof(ReadOnlyAttribute)];
      FieldInfo fi = ra.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
      fi.SetValue(ra, _PropertyA == string.Empty);
    }
  }

  [RefreshProperties(RefreshProperties.All)]
  [ReadOnly(true)]
  public string PropertyB {
    get { return _PropertyB; }
    set { _PropertyB = value; }
  }
}

每当 PropertyA 为空字符串时,这将禁用 PropertyB。

the Code Project找到这篇文章描述了这个过程。

关于.net - 在设计 View 属性网格中禁用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8676996/

相关文章:

asp.net-mvc - MVC 设计模式/架构模式可以用于桌面应用程序开发吗?

C# 缩放 UserControl 内容以匹配用户 Dpi/字体大小

.net - 如何增加.NET 的堆大小?

c# - 如何从 C# 中的预编译器获取当前日期时间?

.net - 事件处理程序的垃圾收集

asp.net - gplV2 : can i use it for free or not?

ios - iOS 应用程序中的 Tategaki(日语竖写)

.Net 在每日构建中检查更新的 AssemblyInfo.cs 是否更好?

c# - 控制音量混合器

c# - 如果可能的话,如何使方法的返回值可选?