Wpf 是否可以向样式中添加自定义拼写检查词典?

Wpf 是否可以向样式中添加自定义拼写检查词典?,wpf,styles,spell-checking,Wpf,Styles,Spell Checking,我发现很多网站都提供了如何将自定义拼写检查词典添加到单个文本框的示例,如: <TextBox SpellCheck.IsEnabled="True" > <SpellCheck.CustomDictionaries> <sys:Uri>customdictionary.lex</sys:Uri> </SpellCheck.CustomDictionaries> </TextBox> cus

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

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

customdictionary.lex
我已经在我的应用程序中进行了测试,效果很好

但是,我需要在应用程序中的所有文本框中忽略特定于行业的术语,并且将此自定义词典单独应用于每个文本框似乎是对样式的挑战。目前,我有一个全局文本框样式来启用拼写检查:

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

我尝试这样做来添加自定义词典,但它不喜欢,因为SpellCheck.CustomDictionary是只读的,setter只接受可写属性

<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>

CustomSpellCheckDictionary.lex

我已经做了大量的搜索来寻找答案,但是所有的例子都只显示了第一个代码块中引用的特定文本框中的一个使用场景。非常感谢您的帮助。

我也遇到过同样的问题,无法以某种方式解决,但创建了一些代码来完成这项工作

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

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);
    }
}
private static void FindAllChildren(DependencyObject父对象,引用列表),其中T:DependencyObject
{
//如有必要,初始化列表
if(list==null)
列表=新列表();
T foundChild=null;
int children=VisualTreeHelper.GetChildrenCount(父级);
//在父对象的可视树中遍历所有子对象并查找匹配项
for(int i=0;i0)
FindAllChildren(儿童,参考列表);
}
}
然后,每当我在应用程序中打开一个新的选项卡/窗口时,我都会向加载的事件添加一个处理程序

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);
     };
window.Loaded+=(对象发送方,RoutedEventArgs e)=>
{
列表文本框=ControlHelper.FindAllChildren((控件)window.Content);
foreach(文本框中的文本框tb)
if(tb.SpellCheck.IsEnabled)
Uri=新的Uri(“pack://application:,,,/MyCustom.lex”);
如果(!tb.SpellCheck.CustomDictionaries.Contains(uri))
tb.SpellCheck.CustomDictionaries.Add(uri);
};