拼写检查数据网格单元

问题描述

我想知道如何动态地使用拼写检查数据网格的每个单元格(不是在xaml中,而是在cs中) 我已经尝试过类似的方法,但是它不起作用。

public static void SpellCheck(System.Windows.Controls.DataGrid MyDataGrid)
    {

        for (int i = 0; i < MyDataGrid.Items.Count; i++)
        {
            for (int j = 0; j < MyDataGrid.Columns.Count; j++)
            {
                System.Windows.Controls.TextBox tb = MyDataGrid.GetCell(i,j).Content as System.Windows.Controls.TextBox;
                tb.SpellCheck.IsEnabled = true;
            }
        }
    }

这种方法称为

SpellCheck(MyDataGrid);

有人可以帮助我吗? 预先谢谢你

解决方法

经过一番研究,我找到了一种解决方案(使用NetSpell),但是需要改进,我可以接受任何建议。谢谢

public static void SpellCheck(System.Windows.Controls.DataGrid MyDataGrid)
    {
        TextBlock content;
        string phrase;
        string[] longtext;
        for (int i = 0; i < MyDataGrid.Items.Count; i++)
        {
            for (int j = 0; j < MyDataGrid.Columns.Count; j++)
            {
                content = MyDataGrid.GetCell(i,j).Content as TextBlock;
                phrase = content.Text;
                longtext = phrase.Split(' ');
                content.Inlines.Clear();
                for (int k = 0; k < longtext.Length; k++)
                {
                    NetSpell.SpellChecker.Dictionary.WordDictionary oDict = new NetSpell.SpellChecker.Dictionary.WordDictionary();

                    oDict.DictionaryFile = "fr-FR.dic";
                    oDict.Initialize();
                    NetSpell.SpellChecker.Spelling oSpell = new NetSpell.SpellChecker.Spelling();

                    oSpell.Dictionary = oDict;
                    if (!oSpell.TestWord(longtext[k]))
                    {
                        //Word does not exist in dictionary
                        content.Inlines.Add(new Run(longtext[k] + " ") { Foreground = Brushes.Red });
                    }
                    else content.Inlines.Add(new Run(longtext[k] + " "));
                }
            }
        }
    }