1

我写了一个自定义的可绑定富文本框,所以我可以绑定到 Document 属性。

但是,一旦我设置了文档内容,它接受的唯一键盘输入就是退格键 (???)。没有其他键盘输入被确认(包括箭头键)。

有任何想法吗?

这是我的 BindableRTB 类的代码:

Imports System.Windows.Documents
Imports System.Windows
Imports System.Windows.Controls

Public Class BindableRTB
    Inherits System.Windows.Controls.RichTextBox




Public Shared DocumentProperty As DependencyProperty = DependencyProperty.Register("Document", GetType(FlowDocument), _
                          GetType(BindableRTB), New FrameworkPropertyMetadata(Nothing, _
                            New PropertyChangedCallback(AddressOf OnDocumentChanged)))
Sub New()
    MyBase.new()
    Me.IsReadOnly = False
    Me.IsDocumentEnabled = True

End Sub

Public Overloads Property Document() As FlowDocument
    Get
        Return CType(MyBase.GetValue(DocumentProperty), FlowDocument)
    End Get
    Set(ByVal value As FlowDocument)
        MyBase.SetValue(DocumentProperty, value)
    End Set
End Property

Private Shared Sub OnDocumentChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)        Console.WriteLine("doc changed")
    Dim rtb As RichTextBox = CType(d, RichTextBox)
    rtb.Document = CType(e.NewValue, FlowDocument)
End Sub

结束类

4

1 回答 1

1

啊哈!解决了。

我没有提到的(因为它似乎不相关,是这个控件在 WPF 窗口中,从 WinForms 应用程序启动)

启动 WPF 窗口时,我需要调用 ElementHost.EnableModelessKeyboardInterop() 并传入对新窗口的引用,如下所示:

 Dim wpfEdit As New WpfEditor
 System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(wpfEdit)
    myParent.ShowNewWPFWindow(wpfEdit)
于 2010-02-03T17:00:09.657 回答