5

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

Thnx 在高级!

4

2 回答 2

11

开箱即用的最佳解决方案是使用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。

于 2016-06-01T20:54:21.527 回答
1

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

或者,您可以实现自己的对话框或使用第三方控件,请参阅WPF 的免费字体和颜色选择器?.

于 2013-06-25T10:19:07.233 回答