我的自定义 WPF RIchTextBox 将不接受用户输入,即使在设置 IsReadOnly=false 后也是如此

发布于 2024-08-20 07:12:02 字数 1281 浏览 3 评论 0原文

我编写了一个自定义可绑定 RichText 框,因此我可以绑定到 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

End Class

I have written a custom Bindable RichText Box, so I can bind to the Document property.

However, as soon as I set my document content, the only keyboard input it accepts is the backspace key (???). No other keyboard input is acknowledged (including the arrow keys).

Any ideas?

Here is the code of my BindableRTB class:

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

End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-08-27 07:12:02

啊哈!解决了。

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

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

 Dim wpfEdit As New WpfEditor
 System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(wpfEdit)
    myParent.ShowNewWPFWindow(wpfEdit)

AH-ha! Solved it.

What I hadn't mentioned (because it didn't seem relevant, was that this control is in a WPF window, launched from a WinForms application)

When launching my WPF window, I needed to call ElementHost.EnableModelessKeyboardInterop() and pass in a reference to my new window, like this:

 Dim wpfEdit As New WpfEditor
 System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(wpfEdit)
    myParent.ShowNewWPFWindow(wpfEdit)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文