2012-04-02 36 views
3

我发现许多网站提供的如何添加自定义的拼写检查字典来个别文本框,像这样的例子:是否可以将自定义拼写检查字典添加到样式中?

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

而且我已经在我的应用程序测试这和它的作品就好了。

但是,我有行业特定的行话,我需要在应用程序中的所有文本框中忽略,并将这个自定义词典单独应用于每个单词似乎吐在面对样式。目前,我有一个全球性的文本样式打开拼写检查:

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

我试图做这样的事情来添加自定义词典,但它不喜欢它,因为SpellCheck.CustomDictionaries是只读而制定者只能使用可写的属性。

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

我已经做了广泛的搜索寻找这个问题的答案,但所有的例子在具体的文本框只显示一个使用方案作为第一个代码块引用。任何帮助表示赞赏。

回答

1

我有同样的问题,不能用风格解决它,但创建了一些代码来完成这项工作。

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

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); 
    } 
} 

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

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); 
    }; 
相关问题