[英]Change the FontStyle in code behind in WPF
如何在WPF中更改代碼隱藏中的FontStyle
。 我試過這個:
listBoxItem.FontStyle = new FontStyle("Italic");
我有錯誤,任何想法?
它是FontStyles.Italic
...使用FontStyles
枚舉來設置FontStyle
的值
listBoxItem.FontStyle = FontStyles.Italic;
試試這個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
。
namespace 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.