我的文本框需要计算出文本框中出现的拼写错误的数量。
我的研究向我展示了如何使用来解决拼写错误
<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/