自定义用户控件中覆盖的字体属性未显示在设计器文件中 [英] Overridden Font property in custom usercontrol not displaying in designer file

查看:27
本文介绍了自定义用户控件中覆盖的字体属性未显示在设计器文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义用户控件并覆盖了它的 Font 属性.

I have a custom user control and have overridden its Font property.

当我将用户控件的副本拖放到表单上时,我可以很好地设置 Font 属性,但是我在表单的设计器文件中看不到我为字体"设置的值.当我编译/运行我的应用程序时,我输入的值丢失了.

When I drop a copy of my usercontrol onto a form, I can set the Font property just fine, but I don't see the value I set for 'Font' showing up in my form's designer file. When I compile/run my app, the value I entered is lost.

请注意,我还想覆盖 Text 属性,但也无法在设计器文件中看到此设置的值——直到我在这里找到答案来帮助做到这一点(我需要设置DesignerSerializationVisibility' 和 'EditorBrowsable' 属性).我试过对 Font 做同样的事情,但无济于事.有什么想法吗?

Note that I also want to override the Text property and couldn't see the set value for this in the designer file either -- until I found an answer on here to help do that (I needed to set the the 'DesignerSerializationVisibility' and 'EditorBrowsable' attributes). I've tried doing the same for Font but to no avail. Any ideas?

    private Font _Font = UserControl.DefaultFont;

    [Description("Sets the font of the button caption"),,
     Browsable(true),
     Bindable(true),
     EditorBrowsable(EditorBrowsableState.Always),
     DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override Font Font
    {
        get { return _Font; }
        set
        {
            _Font = value;
        }

    }

推荐答案

将您的代码更改为:

public UserControl1()
{
  InitializeComponent();
  base.AutoScaleMode = AutoScaleMode.None;
}

[Description("Sets the font of the button caption")]
public override Font Font
{
  get { return _Font; }
  set { _Font = base.Font = value; }
}

这篇关于自定义用户控件中覆盖的字体属性未显示在设计器文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆