【问题标题】:Is this a bug in DotNet 4 WPF Spell Checking?这是 DotNet 4 WPF 拼写检查中的错误吗?
【发布时间】:2012-10-11 15:41:50
【问题描述】:

我有:

  1. 创建了一个 Windows 窗体项目。
  2. 创建了一个用户控件来保存一个 WPF 文本框。
  3. 向文本框的拼写检查添加了自定义字典。
  4. 将 UserControl 添加到我的 Windows 窗体中。

XAML 用于将 WPF 文本框添加到用户控件:

<UserControl x:Class="TestElementHost.SpellBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="248" d:DesignWidth="250">
    <Grid>
        <TextBox Name="txtWPF" />
    </Grid>
</UserControl>

这是表单代码(VS设计器文件中添加的按钮):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestElementHost
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.Integration.ElementHost elementHost1;
        private SpellBox spellBox1;
        public Form1()
        {
            InitializeComponent();
            this.elementHost1 = new System.Windows.Forms.Integration.ElementHost();
            this.spellBox1 = new TestElementHost.SpellBox();
            this.elementHost1.Location = new System.Drawing.Point(27, 12);
            this.elementHost1.Name = "elementHost1";
            this.elementHost1.Size = new System.Drawing.Size(269, 296);
            this.elementHost1.TabIndex = 0;
            this.elementHost1.Text = "elementHost1";
            this.elementHost1.Child = this.spellBox1;
            this.Controls.Add(this.elementHost1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            spellBox1.txtWPF.SpellCheck.IsEnabled = true;
            spellBox1.txtWPF.SpellCheck.CustomDictionaries.Add(new Uri(Application.StartupPath + @"\MyDictionary.lex"));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            spellBox1.txtWPF.Text = "my bbad word."; // bbad is in the CustomDictionary
        }

        private void button2_Click(object sender, EventArgs e)
        {
            spellBox1.txtWPF.IsEnabled = false;
            spellBox1.txtWPF.IsEnabled = true;
           // spellBox1.txtWPF.SpellCheck.IsEnabled = false;
           // spellBox1.txtWPF.SpellCheck.IsEnabled = true;
        }
    }
}

在我尝试更改 IsEnabled、IsReadOnly 或 Visibility 属性之前,这非常有效,并且 CustomDictionary 中的单词被忽略。例如,将 IsReadOnly 设置为 true,然后再次直接返回 false,然后突然将自定义词典中的单词加红线。

当我需要允许用户编辑控件中的文本时,我解决此问题的方法是将 SpellCheck.IsEnabled 设置为 false,然后在连续行上设置为 true。这似乎使习惯词典重新发挥作用。

还有其他人遇到过这个问题吗?这是一个错误还是我错过了什么?

【问题讨论】:

  • 您是使用 XAML 还是仅使用代码将 WPF 文本框添加到 UserControl?
  • TextBox 使用 XAML 添加到 UserControl
  • 嗯,好的,那么初始化错误的可能性会更小 :) 是在 XAML a'la &lt;TextBox x:Name="txtBox" SpellCheck.IsEnabled="True" ... 中设置拼写检查还是在代码中稍后设置?
  • 稍后在代码中设置...我已将 XAML 添加到我的原始帖子中。
  • 我现在也将 Windows 表单代码添加到我的原始帖子中。

标签: c# wpf spell-checking


【解决方案1】:

它看起来像一个“功能”(感谢您验证此 Joachim):

如果您使用自定义字典,然后更改 WPF 文本框或用户控件的可见性、启用或只读属性,拼写检查器将忽略自定义字典。

要解决这个问题,请禁用然后重新启用拼写检查。

这适用于 dot net runtime v4.0.30319

【讨论】:

  • 也许你应该在Microsoft Connect发帖。
  • 好主意,现在就完成了。没有你的链接,我不知道在哪里发帖。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多