c# - 文本框 SpellCheck.IsEnabled - 如何计数

标签 c# wpf

我的文本框需要计算出文本框中出现的拼写错误的数量。

我的研究向我展示了如何使用来解决拼写错误

<TextBox Text="{Binding Content}" SpellCheck.IsEnabled="True" Language="en-GB" />

我有点恼火,因为我不能将 IsReadOnly 设置为 true,但是,我想我必须忍受它。

我不知道如何知道文本框中有多少拼写问题/错误。我能找到的只是http://msdn.microsoft.com/en-us/library/system.windows.controls.spellcheck%28v=vs.110%29.aspx这并没有说确实如此,但我并没有失去希望!

我尝试添加

        TextBox tx = new TextBox();
        tx.SpellCheck.IsEnabled = true;
        tx.Text = "saf and tre";

        var split = tx.Text.Split(' ');
        var errors = 0;
        foreach (var s in split)
        {
            var tempTb = new TextBox();
            tempTb.Text = s;

            SpellingError e = tempTb.GetSpellingError(0); // always null
            var a = tempTb.GetSpellingErrorLength(0);
            var b = tempTb.GetSpellingError(0);
            var c = tempTb.GetSpellingErrorStart(0);

            if ( tempTb.GetSpellingErrorLength(0) >= 0)
                errors++;
        }

如果我更新代码

            SpellingError e = tempTb.GetSpellingError(0); // always null

            SpellingError e = tx.GetSpellingError(0); // not null

然后它会提供建议,然后通知我这是错误的(并且我可以执行计数)。

为了解决我必须做的问题

        TextBox tx = new TextBox();
        tx.SpellCheck.IsEnabled = true;
        tx.Text = "saf many tre further more i sense taht nothing is what is";

        var split = tx.Text.Split(' ');
        var errors = 0;
        var start = 0;
        foreach (var s in split)
        {
            var tempTb = new TextBox();
            tempTb.Text = s;                

            SpellingError f = tx.GetSpellingError(start);

            start += s.Length + 1;

            if (f!=null)
                errors++;
        }

为什么它对 tempTb 不起作用?

最佳答案

从我的调试来看,@EdSF 是正确的,并且必须为临时 TextBox 设置 SpellCheck.IsEnabled

用于重现此内容的代码:

void initTest()
{
    TextBox tx = new TextBox();
    tx.SpellCheck.IsEnabled = true;
    tx.Text = "saf and tre";

    var split = tx.Text.Split(' ');
    var errors = 0;
    foreach (var s in split)
    {
        var tempTb = new TextBox();
        tempTb.SpellCheck.IsEnabled = true;  // Added this line
        tempTb.Text = s;

        SpellingError e = tempTb.GetSpellingError(0); // no longer always null
        var a = tempTb.GetSpellingErrorLength(0);
        var b = tempTb.GetSpellingError(0);
        var c = tempTb.GetSpellingErrorStart(0);

        //if (tempTb.GetSpellingErrorLength(0) >= 0)  //doesn't appear to be correct 
        if (e != null)
        {
            errors++;
        }
    }
}

关于c# - 文本框 SpellCheck.IsEnabled - 如何计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25795507/

相关文章:

c# - 将 View 绑定(bind)到 ViewModel 时 WPF 中的工具提示内存泄漏

c# - 包装数组的集合是只读的。可以在不强制转换的情况下使其可写吗?

wpf - 删除组合框所选项目文本突出显示

c# - 如何禁用按钮,直到在 wpf mvvm 中进行良好验证

c# - 跟踪在 mono asp.net 库中触发的异常的第一步是什么?

WPF 在 CaptureMouse() 之后不发送 MouseMove 事件;

wpf - 根据 Group ItemCount 对组进行排序

c# - 在项目之间共享图像等 Assets 的最佳方式

c# - 有没有办法确定 Convert.ToDateTime 是否添加了年份?

c# - 网络核心 : Convert String to TagBuilder