将 System.Drawing.Font 与 WPF 标签一起使用
我有一个 WPF Label 控件,我试图更改使用由某些旧代码提供的 System.Drawing.Font 对象的外观。 我已经能够设置大部分属性,但在三振和下划线方面遇到困难。
到目前为止我已经:
System.Drawing.Font font = FontFromLegacyCode();
System.Windows.Controls.Label label = new System.Windows.Controls.Label();
label.FontFamily = new System.Windows.Media.FontFamily( font.Name );
label.FontWeight = font.Bold ? System.Windows.FontWeights.Bold : System.Windows.FontWeights.Regular;
label.FontStyle = font.Italic ? System.Windows.FontStyles.Italic : System.Windows.FontStyles.Normal;
label.FontSize = font.Size;
如何设置字体删除线或下划线属性? 有没有更好的控件可以使用?
I have a WPF Label control which I'm trying to change the appearance of using a System.Drawing.Font object supplied by some legacy code. I have been able to set most of the properties, but am struggling with Strikeout and Underline.
So far I have:
System.Drawing.Font font = FontFromLegacyCode();
System.Windows.Controls.Label label = new System.Windows.Controls.Label();
label.FontFamily = new System.Windows.Media.FontFamily( font.Name );
label.FontWeight = font.Bold ? System.Windows.FontWeights.Bold : System.Windows.FontWeights.Regular;
label.FontStyle = font.Italic ? System.Windows.FontStyles.Italic : System.Windows.FontStyles.Normal;
label.FontSize = font.Size;
How do you set the font strikeout or underline properties? Is there a better control to use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看您已有的代码,它可能有问题。
在 MSDN 上的 Windows 窗体和 WPF 属性映射上,他们发表了评论:
Looking at the code you already have, there might be a problem with it.
On the MSDN on Windows Form and WPF Property mapping they make the comment:
我会将其更改为 TextBlock 控件。 TextBlock 控件具有可供使用的 TextDecorations 属性。
或者,如果您确实喜欢,也可以将 TextBlock 粘贴在 Label 内(尽管我只是单独使用 TextBlock )。
查看 TextDecorations 类。
我发现在大多数情况下 TextBlock 比 Label 更合适。 这是一篇关于差异的博客文章。 主要区别在于 Label 是 Control,而 TextBlock 只是 FrameworkElement。 标签还支持访问键。
I would change it to a TextBlock control. The TextBlock control has the TextDecorations property you can use.
Or you can stick a TextBlock inside a Label if you really like (although I'd just use the TextBlock by itself).
Have a look at the TextDecorations class.
I find that TextBlocks are more suitable than Labels in most situations. Here's a blog post about the differences. The chief difference being that a Label is a Control whereas a TextBlock is just a FrameworkElement. Also a Label supports access keys.