conflicting language settings of WPF richtextbox
I have tried to reproduce your problem and for me I could not active spell checker for other language than English, although I have changed Regional Settings and Thread culture before components were initialized:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de-DE");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("de-DE");
Based on solution provided here, I was able to make it work:
1) Inherit from RichTextBox:
class RichTextBoxEx : RichTextBox
{
protected override void OnTextChanged(TextChangedEventArgs e)
{
var changeList = e.Changes.ToList();
if (changeList.Count > 0)
{
foreach (var change in changeList)
{
TextPointer start = null;
TextPointer end = null;
if (change.AddedLength > 0)
{
start = this.Document.ContentStart.GetPositionAtOffset(change.Offset);
end = this.Document.ContentStart.GetPositionAtOffset(change.Offset + change.AddedLength);
}
else
{
int startOffset = Math.Max(change.Offset - change.RemovedLength, 0);
start = this.Document.ContentStart.GetPositionAtOffset(startOffset);
end = this.Document.ContentStart.GetPositionAtOffset(change.Offset);
}
if (start != null && end != null)
{
var range = new TextRange(start, end);
range.ApplyPropertyValue(FrameworkElement.LanguageProperty, Document.Language);
}
}
}
base.OnTextChanged(e);
}
}
2) Use it in your xaml
<local:RichTextBoxEx x:Name="richTextBox" HorizontalAlignment="Left" Height="100" Margin="33,100,0,0" VerticalAlignment="Top" Width="474"
xml:lang="de-DE" SpellCheck.IsEnabled="True">
[edit]
I have also tried to avoid applying the property value for each text change, by defining a timer and spell checking everything from time to time. On my computer, I cannot see the difference when using the longest Wikipedia article content:
class RichTextBoxEx : RichTextBox
{
DispatcherTimer timer;
bool textChanged = false;
public RichTextBoxEx()
{
if (DesignerProperties.GetIsInDesignMode(this))
return;
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
try
{
var range = new TextRange(Document.ContentStart, Document.ContentEnd);
range.ApplyPropertyValue(FrameworkElement.LanguageProperty, Document.Language);
}
finally
{
textChanged = false;
}
}
protected override void OnTextChanged(TextChangedEventArgs e)
{
// TODO: remove if timer version works correctly
//var changeList = e.Changes.ToList();
//if (changeList.Count > 0)
//{
// foreach (var change in changeList)
// {
// TextPointer start = null;
// TextPointer end = null;
// if (change.AddedLength > 0)
// {
// start = this.Document.ContentStart.GetPositionAtOffset(change.Offset);
// end = this.Document.ContentStart.GetPositionAtOffset(change.Offset + change.AddedLength);
// }
// else
// {
// int startOffset = Math.Max(change.Offset - change.RemovedLength, 0);
// start = this.Document.ContentStart.GetPositionAtOffset(startOffset);
// end = this.Document.ContentStart.GetPositionAtOffset(change.Offset);
// }
// if (start != null && end != null)
// {
// var range = new TextRange(start, end);
// range.ApplyPropertyValue(FrameworkElement.LanguageProperty, Document.Language);
// }
// }
//}
textChanged = true;
base.OnTextChanged(e);
}
}
Maybe someone will find this helpful.
I had same problem. I wanted to add spell check in RichTextBox for Serbian Latin and Cyrillic text. To make it work i had to install Windows Language Packs for these two languages. After that this code made it works:
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("sr-Cyrl"); // Change language name to what you need
richTextBox1.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
For languages like German, French, Italian i think you don't need to install languages packs because they are already installed by default, but for other languages you have to.