Is it possible to add a custom style spell checker?

I found many sites that provide examples of adding a custom spell checker dictionary to a separate text box, for example:

<TextBox SpellCheck.IsEnabled="True" >
    <SpellCheck.CustomDictionaries>
        <sys:Uri>customdictionary.lex</sys:Uri>
    </SpellCheck.CustomDictionaries>
</TextBox>

      

And I tested this in my application and it works fine.

However, I have industry-specific jargon that I need to ignore across all of my application's text boxes, and applying this custom vocabulary to each one individually seems to spit styles in the face. At the moment I have a global textbox style to enable spell checking:

<Style TargetType="{x:Type TextBox}">
        <Setter Property="SpellCheck.IsEnabled" Value="True" />
</Style>

      

I tried to do something like this to add a custom dictionary, but he doesn't like it as SpellCheck.CustomDictionaries is read-only and setters are write-only properties.

<Style TargetType="{x:Type TextBox}">
        <Setter Property="SpellCheck.IsEnabled" Value="True" />
        <Setter Property="SpellCheck.CustomDictionaries">
            <Setter.Value>
                <sys:Uri>CustomSpellCheckDictionary.lex</sys:Uri>
            </Setter.Value>
        </Setter>
</Style>

      

I've done extensive searches looking for an answer to this question, but all examples only show a one-off scenario in a specific text field, as stated in the first block of code. Any help is appreciated.

+3


source to share


1 answer


I had the same problem and couldn't solve it with style, but created some code that did the job.

I first created a method to find all the text boxes contained in the visual tree of the parent control.

private static void FindAllChildren<T>(DependencyObject parent, ref List<T> list) where T : DependencyObject
{
    //Initialize list if necessary
    if (list == null)
        list = new List<T>();

    T foundChild = null;
    int children = VisualTreeHelper.GetChildrenCount(parent);

    //Loop through all children in the visual tree of the parent and look for matches
    for (int i = 0; i < children; i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);
        foundChild = child as T;

        //If a match is found add it to the list
        if (foundChild != null)
            list.Add(foundChild);

        //If this control also has children then search it children too
        if (VisualTreeHelper.GetChildrenCount(child) > 0)
            FindAllChildren<T>(child, ref list);
    }
}

      



Then when I open a new tab / window in my application, I add a handler to the loaded event.

window.Loaded += (object sender, RoutedEventArgs e) =>
     {
         List<TextBox> textBoxes = ControlHelper.FindAllChildren<TextBox>((Control)window.Content);
         foreach (TextBox tb in textBoxes)
              if (tb.SpellCheck.IsEnabled)
                  Uri uri = new Uri("pack://application:,,,/MyCustom.lex"));
                       if (!tb.SpellCheck.CustomDictionaries.Contains(uri))
                           tb.SpellCheck.CustomDictionaries.Add(uri);
     };

      

+1


source







All Articles