我的自定义 WPF RIchTextBox 将不接受用户输入,即使在设置 IsReadOnly=false 后也是如此
我编写了一个自定义可绑定 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊哈!解决了。
我没有提到的(因为它似乎不相关,是该控件位于 WPF 窗口中,从 WinForms 应用程序启动)
启动 WPF 窗口时,我需要调用 ElementHost.EnableModelessKeyboardInterop() 并传入引用我的新窗口,如下所示:
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: