我有:
- 创建了一个 Windows 窗体项目。
- 创建了一个用户控件来保存一个 WPF 文本框。
- 向文本框的拼写检查添加了自定义字典。
- 将 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。这似乎使习惯词典重新发挥作用。
有没有其他人有这个问题?这是一个错误还是我错过了什么?