【发布时间】:2012-10-11 15:41:50
【问题描述】:
我有:
- 创建了一个 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。这似乎使习惯词典重新发挥作用。
还有其他人遇到过这个问题吗?这是一个错误还是我错过了什么?
【问题讨论】:
-
您是使用 XAML 还是仅使用代码将 WPF 文本框添加到 UserControl?
-
TextBox 使用 XAML 添加到 UserControl
-
嗯,好的,那么初始化错误的可能性会更小 :) 是在 XAML a'la
<TextBox x:Name="txtBox" SpellCheck.IsEnabled="True" ...中设置拼写检查还是在代码中稍后设置? -
稍后在代码中设置...我已将 XAML 添加到我的原始帖子中。
-
我现在也将 Windows 表单代码添加到我的原始帖子中。
标签: c# wpf spell-checking