C# 在WPF中使用WinForms ColorDialog时出现错误:';颜色';参数类型对格式化属性无效';背景';

C# 在WPF中使用WinForms ColorDialog时出现错误:';颜色';参数类型对格式化属性无效';背景';,c#,wpf,colordialog,C#,Wpf,Colordialog,这个问题已经在网上得到了回答,我正试图按照这些答案来回答,但我仍然得到以下错误问题:我可能做错了什么,我们如何解决 注: 我正在使用fromWindows表单实现wpf中类似的功能 我不想使用第三方工具(WPFToolKit等) WPF相关代码: Using .... using System.Windows.Forms; //for winforms' ColorDialog ...... private void BtnTest_Click(object sender, RoutedEven

这个问题已经在网上得到了回答,我正试图按照这些答案来回答,但我仍然得到以下错误问题:我可能做错了什么,我们如何解决

注:

  • 我正在使用from
    Windows表单
    实现
    wpf中类似的功能
  • 我不想使用第三方工具(WPFToolKit等)
  • WPF相关代码

    Using ....
    using System.Windows.Forms; //for winforms' ColorDialog
    ......
    private void BtnTest_Click(object sender, RoutedEventArgs e)
    {
       ColorDialog MyDialog = new ColorDialog(); //from Winform
       MyDialog.AllowFullOpen = false;
       MyDialog.ShowHelp = true;
    
    if (MyDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        try
        {
            TextSelection textSelection = mainRTB.Selection;
            if (!textSelection.IsEmpty)
            {
                 //Use the WPF System.Windows.Media.Brushes class instead of System.Drawing.Brushes from WinForms:
                textSelection.ApplyPropertyValue(TextElement.BackgroundProperty, ColorHelper.ToSWMColor(MyDialog.Color)); //error occurs at this line
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }
    }
    }
    
    ColorHelper类(我在同一个项目中创建):

    错误

    Using ....
    using System.Windows.Forms; //for winforms' ColorDialog
    ......
    private void BtnTest_Click(object sender, RoutedEventArgs e)
    {
       ColorDialog MyDialog = new ColorDialog(); //from Winform
       MyDialog.AllowFullOpen = false;
       MyDialog.ShowHelp = true;
    
    if (MyDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        try
        {
            TextSelection textSelection = mainRTB.Selection;
            if (!textSelection.IsEmpty)
            {
                 //Use the WPF System.Windows.Media.Brushes class instead of System.Drawing.Brushes from WinForms:
                textSelection.ApplyPropertyValue(TextElement.BackgroundProperty, ColorHelper.ToSWMColor(MyDialog.Color)); //error occurs at this line
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }
    }
    }
    
    “颜色”参数类型对于格式化属性“背景”无效

    类型为
    画笔
    ,而不是
    颜色


    您需要创建一个。

    没有人会读取异常消息:(