xceedsoftware / wpftoolkit

All the controls missing in WPF. Over 1 million downloads.
Other
3.87k stars 871 forks source link

RichTextBox + RichTextBoxFormatBar throwing a NullReference exception when dragged, possible bug? #936

Open xceedsoftware opened 7 years ago

xceedsoftware commented 7 years ago

alex21[CodePlex]
Hello,

I am not sure why but I am receiving the following exception when I select text then attempt to drag the attached RichTextBoxFormatBar: Object reference not set to an instance of an object. at Xceed.Wpf.Toolkit.RichTextBoxFormatBar.ProcessMove(DragDeltaEventArgs e) at Xceed.Wpf.Toolkit.RichTextBoxFormatBar.DragWidget_DragDelta(Object sender, DragDeltaEventArgs e) at System.Windows.Controls.Primitives.DragDeltaEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) etc.... etc... Looking at the source code for RichTextBoxFormatBar: http://wpftoolkit.codeplex.com/SourceControl/latest#Main/Source/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/RichTextBoxFormatBar/RichTextBoxFormatBar.cs

The code for ProcessMove(): private void ProcessMove( DragDeltaEventArgs e ) { AdornerLayer layer = AdornerLayer.GetAdornerLayer( Target ); UIElementAdornerltControlgt adorner = layer.GetAdorners( Target )[ 0 ] as UIElementAdornerltControlgt; adorner.SetOffsets( adorner.OffsetLeft + e.HorizontalChange, adorner.OffsetTop + e.VerticalChange ); } It appears that maybe (I have not built the source myself) the call to AdornerLayer.GetAdornerLayer(Target) is failing to return a value... So I was wondering if there is something I might be missing in my view XAML to get it working correctly? ltToolkit:RichTextBox DockPanel.Dock=quotRightquot Text=quot{Binding Query, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}quotgt ltToolkit:RichTextBox.Stylegt ltStyle TargetType=quot{x:Type Toolkit:RichTextBox}quotgt ... (Note have tried removing the custom style, did not resolve the issue) ... lt/Stylegt lt/Toolkit:RichTextBox.Stylegt ltToolkit:RichTextBox.TextFormattergt ltTextFormatting:RtfToHtmlFormattergtlt/TextFormatting:RtfToHtmlFormattergt lt/Toolkit:RichTextBox.TextFormattergt ltToolkit:RichTextBoxFormatBarManager.FormatBargt ltToolkit:RichTextBoxFormatBargtlt/Toolkit:RichTextBoxFormatBargt lt/Toolkit:RichTextBoxFormatBarManager.FormatBargt lt/Toolkit:RichTextBoxgt Also is there a way to use the RichTextBoxFormatBar with dragging disabled for the meantime?

Thanks, Alex.

xceedsoftware commented 7 years ago

BoucherS[CodePlex]
Hi,

Following your suggestion, this will be fixed in v2.8. Thanks.

xceedsoftware commented 7 years ago

alex21[CodePlex]
Lastly I just noticed this is because I added field validation errors to the property bound to Text which was causing the Validation.ErrorTemplate to show introducing the other layer preventing the UIElementAdornerControl from being found.

So I am 100% this is a bug now, but in the meantime I have just removed the field validation and placed an error message to the right of the control.

xceedsoftware commented 7 years ago

alex21[CodePlex]
Ok after building the source myself I have figured out what is going on but still do not understand it...

The line that is failing to return a value is actually this one: UIElementAdornerControl adorner = layer.GetAdorners(Target)0 as UIElementAdornerControl; Not because layer.GetAdorners(Target) does not return the UIElementAdornerControl, but because it is not at position zero of the array returned...

If I execute the method in the immediate window I get the following output: layer.GetAdorners(Target) {System.Windows.Documents.Adorner[2]}

[1]: {Xceed.Wpf.Toolkit.Core.UIElementAdornerSystem.Windows.Controls.Control}

So I modified the ProcessMove() method to the following to fix this: private void ProcessMove(DragDeltaEventArgs e) { AdornerLayer layer = AdornerLayer.GetAdornerLayer(Target); UIElementAdornerControl adorner = layer.GetAdorners(Target).OfTypeUIElementAdornerControl().First(); adorner.SetOffsets(adorner.OffsetLeft + e.HorizontalChange, adorner.OffsetTop + e.VerticalChange); } Can anyone suggest the best way to proceed? I really do not want to build the source myself as I want to get updates from nuget, but I also need this bug fixed ASAP as it is in a production build.