我写了用户控制(是的!)。但我希望它表现得像一个容器。可是等等!我知道
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design",
typeof(IDesigner))]
Run Code Online (Sandbox Code Playgroud)
诡计。
问题是 - 我不希望我的所有控件都像容器一样,而只是其中的一部分。一个-事实上的-面板;)
为了提供更广泛的上下文:我编写了一个具有网格、一些常见按钮、标签和功能的控件。但它也有一部分用户应该放弃他的自定义按钮/控件。只在这个特定的部分控制,没有其他地方。
有人有任何想法吗?
我有一个PropertyGrid用于在助手类中显示属性的.我将helper类指定为PropertyGrid:
myPropertyGrid.SelectedObject = mySettingsHelper;
Run Code Online (Sandbox Code Playgroud)
在帮助器类中,我ReadOnlyAttribute在设计时分配如下:
[DisplayName("DisplayExA"),
Description("DescriptionExA"),
ReadOnlyAttribute(true)]
public string PropertyA { get; set; }
[DisplayName("DisplayExB"),
Description("DescriptionExB"),
ReadOnlyAttribute(false)]
public string PropertyB { get; set; }
[DisplayName("DisplayExC"),
Description("DescriptionExC"),
ReadOnlyAttribute(true)]
public string PropertyC { get; set; }
Run Code Online (Sandbox Code Playgroud)
但是现在我需要能够在运行时动态地更改各个属性的这个属性.根据某些标准,这些属性中的一些可能需要是只读的或不是只读的.如何在运行时动态进行更改?
编辑:
我尝试了以下代码,但这为对象的每个实例设置了ReadOnly属性!我想按对象做.有时一个对象可能具有PropertyA只读,而第二个对象的PropertyA不是只读的.
public static class PropertyReadOnlyHelper
{
public static void SetReadOnly(object container, string name, bool value)
{
try
{
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(container.GetType())[name];
ReadOnlyAttribute attribute = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
FieldInfo fieldToChange = attribute.GetType().GetField("isReadOnly",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
fieldToChange.SetValue(attribute, value);
}
catch …Run Code Online (Sandbox Code Playgroud) 我创建了一个Button后代,其中隐藏了所有不使用的属性。
我这样做是这样的:
[Browsable(false)]
[Bindable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Obsolete("", true)]
public new Boolean AllowDrop { get; set; }
Run Code Online (Sandbox Code Playgroud)
大多数属性被正确隐藏,无法使用。
但是,有两个属性我无法摆脱。
有没有办法在设计器中也删除GenerateMember和Modifiers?
c# propertygrid windows-forms-designer visual-studio winforms