Is this a bug in DotNet 4 WPF Spell Checking?

徘徊边缘 提交于 2020-01-03 03:31:12

问题


I have:

  1. Created a Windows forms project.
  2. Created a UserControl to hold a WPF TextBox.
  3. Added a CustomDictionary to the TextBox's SpellCheck.
  4. Added the UserControl to my Windows Form.

XAML is used to add the WPF textbox to the user control:

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

Here is the form code (buttons added in the VS designer file):

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

This works quite happily and words in the CustomDictionary are ignored, until I try changing the IsEnabled, IsReadOnly or the Visibility property. For example, set IsReadOnly to true then straight back to false again and suddenly words that are in the customdictionary are redlined.

The way I am getting round this is to set SpellCheck.IsEnabled to false then back to true on consecutive lines when I need to allow users to edit text in the control. This seems to bring the customdictionary back into play.

Has anyone else had this problem? Is this a bug or have I missed something?


回答1:


It looks like a "feature" (thanks for verifying this Joachim):

If you are using the custom dictionary, then change the visbility, enabled or read only attributes of the WPF textbox or the user control, the spell checker ignores the custom dictionary.

To get round this, disable then re-enable spell checking.

This applies to dot net runtime v4.0.30319



来源:https://stackoverflow.com/questions/13001118/is-this-a-bug-in-dotnet-4-wpf-spell-checking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!