我该如何在WPF的代码后端更改FontStyle
?我尝试了这个:
listBoxItem.FontStyle = new FontStyle("Italic");
我遇到了错误,有什么想法吗?
它是FontStyles.Italic
... 使用FontStyles
枚举来设置FontStyle
的值
listBoxItem.FontStyle = FontStyles.Italic;
listBoxItem.FontStyle = FontStyles.Italic;
FontStyle
是结构体 MSDN
:
可以在定义了一个表示字体样式的结构体,包括正常、斜体或倾斜。
ILSpy
中查看。[TypeConverter(typeof(FontStyleConverter)), Localizability(LocalizationCategory.None)]
public struct FontStyle : IFormattable
{
private int _style;
internal FontStyle(int style)
{
this._style = style;
}
Int
的字段_style
。要设置Int
类型的值,需要从静态类FontStyles
中获取:public static class FontStyles
{
public static FontStyle Normal
{
get
{
return new FontStyle(0);
}
}
public static FontStyle Oblique
{
get
{
return new FontStyle(1);
}
}
public static FontStyle Italic
{
get
{
return new FontStyle(2);
}
}
internal static bool FontStyleStringToKnownStyle(string s, IFormatProvider provider, ref FontStyle fontStyle)
{
if (s.Equals("Normal", StringComparison.OrdinalIgnoreCase))
{
fontStyle = FontStyles.Normal;
return true;
}
if (s.Equals("Italic", StringComparison.OrdinalIgnoreCase))
{
fontStyle = FontStyles.Italic;
return true;
}
if (s.Equals("Oblique", StringComparison.OrdinalIgnoreCase))
{
fontStyle = FontStyles.Oblique;
return true;
}
return false;
}
}
FontStyle
需要引用静态类FontStyles
:SomeControl.FontStyle = FontStyles.Italic;
可能有点混淆,实际上有两个FontStyle
(没有s)枚举:
namespace MS.Internal.Text.TextInterface
internal enum FontStyle
{
Italic = 2,
Oblique = 1,
Normal = 0
}
FontStyles
在系统内部一起使用。
命名空间 System.Drawing
[Flags]
public enum FontStyle
{
Regular = 0,
Bold = 1,
Italic = 2,
Underline = 4,
Strikeout = 8
}
System.Drawing
中使用,如下所示:SomeControl.Font = new Font(FontFamily.GenericSansSerif,
12.0F, FontStyle.Bold | FontStyle.Italic);
s
.Italic;。 - Anatoliy Nikolaev