支持注释的 WPF 文档查看控件包括 FlowDocumentReader 和 FlowDocumentScrollViewer,以及从 DocumentViewerBase 派生的控件,如 DocumentViewer 和 FlowDocumentPageViewer。
The in-built Annotations support for Document based controls is awesome in WPF
我想知道如何将功能添加到 WPF 通用控件(如按钮、文本框、列表框项目等)中。想法是允许用户在 UI 屏幕上将一些评论传递给另一个用户。
首先想到的是从 DocumentViewerBase 继承并创建您自己的自定义控件。我不确定它会如何运作。如果非自定义控件需要注释怎么办?
有没有人工作或见过这种功能?
任何方向都会有所帮助。
最佳答案
嗯。我可能会用 adorner 来做这件事:
Imports System.Windows
Imports System.Windows.Documents
Imports System.Windows.Media
Public Class Annotation
Inherits Adorner
Private _fill As Brush
Private _pen As Pen
Private _text As FormattedText
Private _annotationText as String
Public Sub New(ByVal adornedElement As UIElement, ByVal annotationText as String)
MyBase.New(adornedElement)
_annotationText = annotationText
_fill = New SolidColorBrush(Color.FromArgb(&H33, &HB0, &HC4, &HDE))
_fill.Freeze()
_pen = New Pen(Brushes.LightSteelBlue, 3.0)
_pen.Freeze()
_text = New FormattedText(_annotationText, Globalization.CultureInfo.InvariantCulture, FlowDirection.LeftToRight, New Typeface("Verdana"), 11.0, Brushes.Black)
Me.IsHitTestVisible = False
End Sub
Protected Overrides Sub OnRender(ByVal drawingContext As DrawingContext)
MyBase.OnRender(drawingContext)
Dim adornedRect As New Rect(MyBase.AdornedElement.RenderSize)
drawingContext.DrawRectangle(_fill, _pen, adornedRect)
drawingContext.DrawText(_text, New Point(0,0))
End Sub
End Class
然后你可以通过以下方式使用它:
Private Sub AddAnnotation(ByVal uie As UIElement, ByVal annotationText as String)
Dim annotation = New Annotation(uie)
Dim adornerLayer = AdornerLayer.GetAdornerLayer(uie, annotationText)
adornerLayer.Add(annotation)
End Sub
我会让你调整注释的位置和实际外观,但你明白了。这适用于 任何 UIElement,包括自定义控件。
这是基于我与 Adorners 完成的其他一些工作的即兴回答。上面的代码可能会也可能不会编译。我没有提供编辑注释的方法,但您可以通过删除“Me.IsHitTestVisible = False”行并在装饰器中处理 MouseUp 事件来轻松完成。
关于.net - 如何允许用户在 WPF 中向 UI 控件添加注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/507383/