【问题标题】:Overridden Font property in custom usercontrol not displaying in designer file自定义用户控件中的重写字体属性未显示在设计器文件中
【发布时间】:2011-08-02 17:21:35
【问题描述】:

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

当我将用户控件的副本拖放到表单上时,我可以很好地设置 Font 属性,但我没有看到我为“Font”设置的值出现在表单的设计器文件中。当我编译/运行我的应用程序时,我输入的值会丢失。

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

    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;
        }

    }

【问题讨论】:

  • 你不能只用base.Font吗?有两个实例有什么意义?
  • 在文档中也注意到this:“ 继承者注意事项 在派生类中重写 Font 属性时,请使用基类的 Font 属性来扩展基实现。否则,您必须提供所有实现。您不需要同时覆盖 Font 属性的 get 和 set 访问器;如果需要,您可以只覆盖一个。 "

标签: c# winforms


【解决方案1】:

将您的代码更改为:

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; }
}

【讨论】:

  • 这确实将值放入设计器文件中,但是设置基本字体属性会使我的用户控件上的所有子控件的大小失控。例如,如果我增加字体大小,我在用户控件上的按钮将会增加。
  • @Mike R. 更新答案:将 AutoScaleMode = Font(默认)设置为 None。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 2016-01-29
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多