如何覆盖TextBlock.GetTextPositionFromPoint?
问题描述:
是否有可能重写方法如何覆盖TextBlock.GetTextPositionFromPoint?
public TextPointer GetPositionFromPoint(Point point, bool snapToText);
一个WPF的TextBlock的
?
我的类从TextBlock的继承,但我得到一个例外,如果我设置
TextTrimming="CharacterEllipsis"
和移动鼠标点(“...”)。
Exception:
Message: The requested distance is outside the content of the associated document.
Stacktrace:
bei System.Windows.Documents.TextPointer.InitializeOffset(TextPointer position, Int32 distance, LogicalDirection direction)
bei System.Windows.Documents.TextPointer.System.Windows.Documents.ITextPointer.CreatePointer(Int32 offset, LogicalDirection gravity)
bei System.Windows.Controls.TextBlock.GetTextPositionFromDistance(Int32 dcp, Double distance, Double lineVOffset, Int32 index)
bei MS.Internal.Text.TextLineResult.GetTextPositionFromDistance(Double distance)
bei MS.Internal.Documents.TextParagraphView.GetTextPositionFromPoint(ReadOnlyCollection`1 lines, Point point, Boolean snapToText)
bei MS.Internal.Documents.TextParagraphView.GetTextPositionFromPoint(Point point, Boolean snapToText)
bei MS.Internal.Documents.TextViewBase.System.Windows.Documents.ITextView.GetTextPositionFromPoint(Point point, Boolean snapToText)
bei System.Windows.Documents.TextEditorMouse.IsPointWithinInteractiveArea(TextEditor textEditor, Point point)
bei System.Windows.Documents.TextEditorMouse.OnQueryCursor(Object sender, QueryCursorEventArgs e)
bei System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
bei System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
bei System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
bei System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
bei System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
bei System.Windows.Input.InputManager.ProcessStagingArea()
bei System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
bei System.Windows.Input.MouseDevice.UpdateCursorPrivate()
bei System.Windows.Input.MouseDevice.PostProcessInput(Object sender, ProcessInputEventArgs e)
bei System.Windows.Input.InputManager.RaiseProcessInputEventHandlers(ProcessInputEventHandler postProcessInput, ProcessInputEventArgs processInputEventArgs)
bei System.Windows.Input.InputManager.ProcessStagingArea()
bei System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
bei System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
bei System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
bei System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
bei System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
bei MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
捕捉异常我想overide base.GetPositionFromPoint(Point point,bool snapToText)。
但以下从来没有被击中:
public new TextPointer GetPositionFromPoint(Point point, bool snapToText) {
try {
return base.GetPositionFromPoint(point, snapToText);
}
catch (Exception) {
return null;
}
}
有什么不对?
答
是否有可能重写WPF TextBlock的方法GetPositionFromPoint?
不可以。您不能覆盖未标记为virtual
的方法。
你的确可以隐藏使用new
关键字的方法,但是这不会让你的虚拟和重载方法的多态behvaiour,即WPF不会打电话给你的方法,这就是为什么你的断点永远不会被打。
该方法未标记为“虚拟”,因此您无法覆盖它 – Evk