如何让SelectableTextBlock(WPF)与TextTrimming一起使用?

时间:2017-09-13 15:00:32

标签: c# wpf textbox

我找到了一个非常有用的帖子,为可选择的wpf文本框提供了一个解决方案:

SelectableTextBlock (by torvin)

TextEditorWrapper很棒,

但是如果我将TextTrimming属性设置为例如TextTrimming="CharacterEllipsis",如果鼠标移过...,则会发生异常。

例外:

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)

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

问题是因为当你在省略号上给它一个点时,TextBlock.GetPositionFromPoint()会抛出异常。最简单的方法是将它放在try / catch中,并修复OnMouseButtonUp。

没有公开的方法来判断鼠标是否在省略号上。如果你总是单行TextBlock,那么它很简单......只需使用FormattedText()测量字符串,看看它是否宽于ActualWidth,然后不要调用该方法。

如果您正在谈论多行TextBlock,那么您将陷入困境......