是否可以向样式添加自定义拼写检查字典? [英] Is it possible to add a custom spellchecking dictionary to a style?

查看:30
本文介绍了是否可以向样式添加自定义拼写检查字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现许多网站提供了如何将自定义拼写检查字典添加到单个文本框的示例,如下所示:

I've found numerous sites that provide examples of how to add a custom spellchecking dictionary to an individual textbox like so:

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

而且我已经在我的应用程序中测试了它并且它工作得很好.

And I've tested this in my app and it works just fine.

但是,我有行业特定的行话,我需要在应用程序中的所有文本框中忽略这些行话,并且将这个自定义字典单独应用于每个文本框似乎与样式不同.目前我有一个全局文本框样式来打开拼写检查:

However, I have industry specific jargon that I need to have ignored across ALL the textboxes in the application and applying this custom dictionary to each one individually seems to spit in the face of styles. At the moment I have a global textbox style to turn on spellchecking:

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

我尝试做这样的事情来添加自定义字典,但它不喜欢它,因为 SpellCheck.CustomDictionaries 是只读的,而 setter 只采用可写属性.

I tried to do something like this to add the custom dictionary, but it doesn't like it, since the SpellCheck.CustomDictionaries is read only and setters only take writable 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 have done extensive searches looking for the answer to this, but all examples show only the one-use scenario in the specific textbox as cited in the first code block. Any help is appreciated.

推荐答案

我遇到了同样的问题,无法通过样式解决,但创建了一些代码来完成工作.

I had the same issue and couldn't solve it with a style but created some code that accomplished the job.

首先,我创建了一个方法来查找父控件的可视化树中包含的所有文本框.

First, I created a method to find all the textboxes contained within the visual tree of a 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's children too
        if (VisualTreeHelper.GetChildrenCount(child) > 0)
            FindAllChildren<T>(child, ref list);
    }
}

然后,每当我在我的应用程序中打开一个新选项卡/窗口时,我都会向加载的事件添加一个处理程序.

Then, anytime 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);
     };

这篇关于是否可以向样式添加自定义拼写检查字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆