Pm *_*uda 5 .net c# custom-controls windows-forms-designer winforms
我正在尝试从 System.Windows.Forms.Button
public class MyButton : Button
{
    public MyButton() : base()
    {
        Size = new Size(100, 200);
    }
    [DefaultValue(typeof(Size), "100, 200")]
    public new Size Size { get => base.Size; set => base.Size = value; }
}
Designer.cs行为有问题- 默认值无法正常工作。
我希望,当MyButton被添加到表单时,它的大小为 100x200,但它不是通过Designer.cs设置的,所以在MyButton构造函数中我将大小更改为 200x200(也为 DefaultValue)都MyButton获得新的大小。当然,当我在设计模式中更改大小时,它应该被添加到Designer.cs 中,并且不受MyButton类中以后更改的影响。
尽管在当前配置中 Size 始终添加到Designer.cs。
我尝试了不同的方法(使用 Invalidate() 或 DesignerSerializationVisibility),但没有成功。
Size当它等于 时,我想停止被序列化DefaultValue。例如,当它从工具箱放到表单中时 - 它会立即在设计器中序列化,而我不希望那样 - 仅在我更改大小时进行序列化。
由于某种原因,将中的属性ControlDesigner替换为始终返回的自定义属性描述符。这意味着该属性将始终被序列化,除非您使用具有隐藏值的设计器序列化可见性属性来装饰它。SizePreFilterPropertiesShouldSerializeValuetrueSize
您可以通过恢复原始属性描述符来更改行为:
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
[Designer(typeof(MyButtonDesigner))]
public class MyButton : Button
{
    protected override Size DefaultSize
    {
        get { return new Size(100, 100); }
    }
    //Optional, just to enable Reset context menu item
    void ResetSize()
    {
        Size = DefaultSize;
    }
}
public class MyButtonDesigner : ControlDesigner
{
    protected override void PreFilterProperties(IDictionary properties)
    {
        var s = properties["Size"];
        base.PreFilterProperties(properties);
        properties["Size"] = s;
    }
}
| 归档时间: | 
 | 
| 查看次数: | 177 次 | 
| 最近记录: |