我正在尝试使用C#提供的SpellCheck类(在PresentationFramework.dll中)。但是,尝试将拼写绑定到文本框时遇到问题:
SpellCheck.SetIsEnabled(txtWhatever, true);
问题是我的txtWhat是System.Windows.Forms类型,此函数正在查找的参数是System.Windows.Controls,并且简单转换失败。我也试图使我的TextBox成为这种类型,但是……不能。有人知道如何使用此SpellCheck对象吗?(MSDN并没有帮助…)
谢谢
您必须使用WPF TextBox才能进行拼写检查。您可以使用ElementHost控件将其嵌入Windows窗体表单中。它的工作原理与UserControl非常相似。这是一个您可以直接从工具箱中放下的控件。首先,您需要Project + Add Reference,然后选择WindowsFormsIntegration,System.Design和WPF程序集PresentationCore,PresentationFramework和WindowsBase。
将新类添加到您的项目中,然后粘贴以下代码。编译。将SpellBox控件从工具箱的顶部拖放到窗体上。它支持TextChanged事件以及Multiline和WordWrap属性。字体存在困扰,没有简单的方法可以将WF字体映射到WPF字体属性。最简单的解决方法是将窗体的Font设置为WPF的默认“ Segoe UI”。
using System; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Windows; using System.Windows.Controls; using System.Windows.Forms.Integration; using System.Windows.Forms.Design; [Designer(typeof(ControlDesigner))] //[DesignerSerializer("System.Windows.Forms.Design.ControlCodeDomSerializer, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")] class SpellBox : ElementHost { public SpellBox() { box = new TextBox(); base.Child = box; box.TextChanged += (s, e) => OnTextChanged(EventArgs.Empty); box.SpellCheck.IsEnabled = true; box.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; this.Size = new System.Drawing.Size(100, 20); } public override string Text { get { return box.Text; } set { box.Text = value; } } [DefaultValue(false)] public bool Multiline { get { return box.AcceptsReturn; } set { box.AcceptsReturn = value; } } [DefaultValue(false)] public bool WordWrap { get { return box.TextWrapping != TextWrapping.NoWrap; } set { box.TextWrapping = value ? TextWrapping.Wrap : TextWrapping.NoWrap; } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public new System.Windows.UIElement Child { get { return base.Child; } set { /* Do nothing to solve a problem with the serializer !! */ } } private TextBox box; }
根据普遍的需求,此代码的VB.NET版本避免使用lambda:
Imports System Imports System.ComponentModel Imports System.ComponentModel.Design.Serialization Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Forms.Integration Imports System.Windows.Forms.Design <Designer(GetType(ControlDesigner))> _ Class SpellBox Inherits ElementHost Public Sub New() box = New TextBox() MyBase.Child = box AddHandler box.TextChanged, AddressOf box_TextChanged box.SpellCheck.IsEnabled = True box.VerticalScrollBarVisibility = ScrollBarVisibility.Auto Me.Size = New System.Drawing.Size(100, 20) End Sub Private Sub box_TextChanged(ByVal sender As Object, ByVal e As EventArgs) OnTextChanged(EventArgs.Empty) End Sub Public Overrides Property Text() As String Get Return box.Text End Get Set(ByVal value As String) box.Text = value End Set End Property <DefaultValue(False)> _ Public Property MultiLine() As Boolean Get Return box.AcceptsReturn End Get Set(ByVal value As Boolean) box.AcceptsReturn = value End Set End Property <DefaultValue(False)> _ Public Property WordWrap() As Boolean Get Return box.TextWrapping <> TextWrapping.NoWrap End Get Set(ByVal value As Boolean) If value Then box.TextWrapping = TextWrapping.Wrap Else box.TextWrapping = TextWrapping.NoWrap End If End Set End Property <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _ Public Shadows Property Child() As System.Windows.UIElement Get Return MyBase.Child End Get Set(ByVal value As System.Windows.UIElement) '' Do nothing to solve a problem with the serializer !! End Set End Property Private box As TextBox End Class