在 WPF 后面的代码中更改 FontStyle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22033427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the FontStyle in code behind in WPF
提问by John Jerrby
How can I change the FontStylein the code-behind in WPF. I tried this:
如何更改FontStyleWPF 中的代码隐藏。我试过这个:
listBoxItem.FontStyle = new FontStyle("Italic");
and I got error, any idea?
我有错误,知道吗?
回答by Sankarann
It was FontStyles.Italic... Use the FontStylesenum to set the value for FontStyle
它是FontStyles.Italic......使用FontStyles枚举设置值FontStyle
listBoxItem.FontStyle = FontStyles.Italic;
回答by Nagaraj S
Try this FontStyles.Italic
尝试这个 FontStyles.Italic
listBoxItem.FontStyle = FontStyles.Italic;
回答by Anatoliy Nikolaev
In this situation FontStyleis structure MSDN:
在这种情况下FontStyle是结构MSDN:
Defines a structure that represents the style of a font face as normal, italic, or oblique.
定义将字体样式表示为正常、斜体或斜体的结构。
It can be viewed in ILSpy:
它可以在ILSpy:
[TypeConverter(typeof(FontStyleConverter)), Localizability(LocalizationCategory.None)]
public struct FontStyle : IFormattable
{
private int _style;
internal FontStyle(int style)
{
this._style = style;
}
Here we see that the field _styleof type Int. To set the value of Inttype , it is taken from the static class FontStyles:
在这里我们看到_styletype的字段Int。要设置Inttype的值,它取自静态类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;
}
}
So it turns out, to setting FontStyleneed to refer to a static class FontStyles:
所以事实证明,设置FontStyle需要引用一个静态类FontStyles:
SomeControl.FontStyle = FontStyles.Italic;
There can be a bit confusing, in fact there are two FontStyle(without s) enumerations:
可能有点混乱,实际上有两个FontStyle(没有 s)枚举:
namespace MS.Internal.Text.TextInterface
namespace MS.Internal.Text.TextInterface
internal enum FontStyle
{
Italic = 2,
Oblique = 1,
Normal = 0
}
This enumeration are Internaland I think used inside the system in conjunction with an public structure FontStyles.
这个枚举是Internal 的,我认为在系统内部与公共结构一起使用FontStyles。
namespace System.Drawing
namespace System.Drawing
[Flags]
public enum FontStyle
{
Regular = 0,
Bold = 1,
Italic = 2,
Underline = 4,
Strikeout = 8
}
This flags enumeration is Publicand used in System.Drawinglike this:
这个标志枚举是公共的,并且System.Drawing像这样使用:
SomeControl.Font = new Font(FontFamily.GenericSansSerif,
12.0F, FontStyle.Bold | FontStyle.Italic);

