2

Is it possible to override the method

public TextPointer GetPositionFromPoint(Point point, bool snapToText);

of a WPF TextBlock?

My class inherits from TextBlock, but I get an exception, if I set

TextTrimming="CharacterEllipsis" 

and move the mouse over the dots ("...").

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)

To catch the exceptionI would like to overide base.GetPositionFromPoint(Point point, bool snapToText).

But the following never gets hit:

public new TextPointer GetPositionFromPoint(Point point, bool snapToText) {
  try {
    return base.GetPositionFromPoint(point, snapToText);
  }
  catch (Exception) {
    return null;
  }
}

What's wrong?

cew3
  • 73
  • 6

1 Answers1

0

Is it possible to override the method GetPositionFromPoint of a WPF TextBlock?

No. You cannot override a method that is not marked as virtual.

You can indeed hide the method using the new keyword but this won't get you the polymorphic behvaiour of a virtual and overridden method, i.e. WPF won't call your method, and that's why your breakpoint never gets hit.

Is it possible to override a non-virtual method?

mm8
  • 163,881
  • 10
  • 57
  • 88