如何将 DefaultValueAtribute class 用于 Visual Studio 中的字体类型?
How to use DefaultValueAtribute class for a Font type in Visual Studio?
我想知道我必须使用哪种正确的字符串语法来为 DefaultValueAtribute class 指定一个值 Font 类型,以便在 Visual Studio 的设计器 属性 网格中将该值显示为非粗体。
这是我试过的:
C#:
public class MyControl : UserControl {
[DefaultValue(typeof(Font), "Microsoft Sans Serif, 8.25pt")]
public override Font Font {
get { }
set { }
}
}
VB.NET:
Public Class MyControl : Inherits UserControl
<DefaultValue(GetType(Font), "Microsoft Sans Serif, 8.25pt")>
Public Overrides Property Font As Font
...
End Property
End Class
...但是,默认字体字符串显示在 Bold 中 Visual Studio's 属性 我的控件的网格。
请注意,我显然是在搜索正确的解析字符串,而不是 Reflection or ShouldSerializeFOO 棘手的方法。
使用带有 PropertyGrid 的表单对此进行了测试。也许您没有在私有支持字段中设置初始值?
Public Class MainWindow
Private Sub MainWindow_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Dim MC As New MyControl
Me.PropertyGrid1.SelectedObject = MC
End Sub
Public Class MyControl : Inherits UserControl
Private _Font As Font = New Font("Microsoft Sans Serif", 8.25)
<DefaultValue(GetType(Font), "Microsoft Sans Serif, 8.25")>
Public Overrides Property Font() As Font
Get
Return _Font
End Get
Set
_Font = Value
End Set
End Property
End Class
End Class
您似乎只是忘记将 Font
属性 的初始值设置为默认值,因此控件将使用其父字体,这与您想要的默认值不同,并且将以粗体显示。
您可以这样设置字体默认值:
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public partial class MyControl : UserControl
{
private const string MyDefaultFont = "Tahoma, 10pt";
public MyControl()
{
InitializeComponent();
this.Font = (Font)new FontConverter().ConvertFromString(MyDefaultFont);
}
[DefaultValue(typeof(Font), MyDefaultFont)]
public override Font Font
{
get { return base.Font; }
set { base.Font = value; }
}
}
注意: Control.Font
是 ambient property 并且如果您没有明确地为 Font
属性 分配任何值,那么它将不会被序列化,控件将使用其父级 Font
属性。如果您希望某些控件使用与其父控件 Font
不同的字体,为其指定字体就足够了。因此,您似乎根本不需要为子控件分配任何默认字体。
我想知道我必须使用哪种正确的字符串语法来为 DefaultValueAtribute class 指定一个值 Font 类型,以便在 Visual Studio 的设计器 属性 网格中将该值显示为非粗体。
这是我试过的:
C#:
public class MyControl : UserControl {
[DefaultValue(typeof(Font), "Microsoft Sans Serif, 8.25pt")]
public override Font Font {
get { }
set { }
}
}
VB.NET:
Public Class MyControl : Inherits UserControl
<DefaultValue(GetType(Font), "Microsoft Sans Serif, 8.25pt")>
Public Overrides Property Font As Font
...
End Property
End Class
...但是,默认字体字符串显示在 Bold 中 Visual Studio's 属性 我的控件的网格。
请注意,我显然是在搜索正确的解析字符串,而不是 Reflection or ShouldSerializeFOO 棘手的方法。
使用带有 PropertyGrid 的表单对此进行了测试。也许您没有在私有支持字段中设置初始值?
Public Class MainWindow
Private Sub MainWindow_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Dim MC As New MyControl
Me.PropertyGrid1.SelectedObject = MC
End Sub
Public Class MyControl : Inherits UserControl
Private _Font As Font = New Font("Microsoft Sans Serif", 8.25)
<DefaultValue(GetType(Font), "Microsoft Sans Serif, 8.25")>
Public Overrides Property Font() As Font
Get
Return _Font
End Get
Set
_Font = Value
End Set
End Property
End Class
End Class
您似乎只是忘记将 Font
属性 的初始值设置为默认值,因此控件将使用其父字体,这与您想要的默认值不同,并且将以粗体显示。
您可以这样设置字体默认值:
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public partial class MyControl : UserControl
{
private const string MyDefaultFont = "Tahoma, 10pt";
public MyControl()
{
InitializeComponent();
this.Font = (Font)new FontConverter().ConvertFromString(MyDefaultFont);
}
[DefaultValue(typeof(Font), MyDefaultFont)]
public override Font Font
{
get { return base.Font; }
set { base.Font = value; }
}
}
注意: Control.Font
是 ambient property 并且如果您没有明确地为 Font
属性 分配任何值,那么它将不会被序列化,控件将使用其父级 Font
属性。如果您希望某些控件使用与其父控件 Font
不同的字体,为其指定字体就足够了。因此,您似乎根本不需要为子控件分配任何默认字体。