【问题标题】:How to open Color and Font Dialog box using WPF?如何使用 WPF 打开颜色和字体对话框?
【发布时间】:2013-06-22 01:36:51
【问题描述】:

我想在 WPF .net 4.5 中显示颜色和字体对话框,我该怎么做? 请任何人帮助我。

Thnx 在高级!

【问题讨论】:

标签: wpf dialog


【解决方案1】:

开箱即用的最佳解决方案是使用FontDialog 形式System.Windows.Forms 程序集,但您必须转换其输出以将其应用于 WPF 元素。

FontDialog fd = new FontDialog();
var result = fd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
    Debug.WriteLine(fd.Font);

    tbFonttest.FontFamily = new FontFamily(fd.Font.Name);
    tbFonttest.FontSize = fd.Font.Size * 96.0 / 72.0;
    tbFonttest.FontWeight = fd.Font.Bold ? FontWeights.Bold : FontWeights.Regular;
    tbFonttest.FontStyle = fd.Font.Italic ? FontStyles.Italic : FontStyles.Normal;

    TextDecorationCollection tdc = new TextDecorationCollection();
    if (fd.Font.Underline) tdc.Add(TextDecorations.Underline);
    if (fd.Font.Strikeout) tdc.Add(TextDecorations.Strikethrough);
    tbFonttest.TextDecorations = tdc;
}

请注意,winforms 对话框不支持许多 WPF 字体属性,例如加粗字体。

颜色对话框更容易:

ColorDialog cd = new ColorDialog();
var result = cd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
    tbFonttest.Foreground = new SolidColorBrush(Color.FromArgb(cd.Color.A, cd.Color.R, cd.Color.G, cd.Color.B));
}

虽然它不支持 alpha。

【讨论】:

    【解决方案2】:

    您可以使用来自System.Windows.Forms 的类,使用它们没有任何问题。不过,您可能需要将值转换为特定于 WPF 的值。

    或者,您可以实现自己的对话框或使用第三方控件,请参阅Free font and color chooser for WPF?

    【讨论】:

      猜你喜欢
      • 2018-10-31
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2023-02-04
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      相关资源
      最近更新 更多