You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you set the CaretPosition property of a RichTextBox programmatically that contains wrapped text, subsequent keyboard movements to the next or previous "line" don't behave correctly.
This is due to the internal TextEditor's _suggestedX member not being cleared correctly (it has the default value 0 and should be double.NaN so that internal keyboard navigations (e.g. by "OnMoveDownByLine(...)") are handled correctly. This is because the GetSuggestedX method requires the current value to be set to double.NaN in order to re-calculate the value correctly.
I wonder if "TextEditorSelection.ClearSuggestedX(TextEditor)" needs to be called automatically whenever the CaretPosition setter is used.
Reproduction Steps
Build a WPF Application (I used .NET 8):
<Window x:Class="RTB_SuggestX_Issue.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RTB_SuggestX_Issue"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row ="0" Content="Click Me" VerticalAlignment="Top" Click="ButtonBase_OnClick"/>
<RichTextBox Name="rtb" Grid.Row ="1" HorizontalAlignment="Left" Width="200">
<FlowDocument>
<Paragraph>
<Run Text="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua..."/>
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
</Window>
using System.Windows;
namespace RTB_SuggestX_Issue
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
rtb.Focus();
rtb.CaretPosition = rtb.GetPositionFromPoint(new Point(100, 0), true);
}
}
}
Click the Button
Press the ArrowKey Down next and observe that the caret is placed at position 0 instead of 100
Note that this problem also persists in other scenarios, e.g:
Restart the application
Click the button
Press the ArrowKey Down next and observe that the caret is placed at position 0 instead of 100
Move the Caret left or right with the arrow keys (but stay in the second line)
Click the button again
Press the ArrowKey Down abd observe that the cache seems to have been cleared correctly for this movement
move the caret left/right again with the arrow keys (but stay in the second line), remember this position, make it different from the starting position
Click the button again
Press the ArrowKey Down and observe that the caret is placed at the position of step 7
Expected behavior
The caret always behaves correctly, even if the CaretPosition was set programmatically.
Actual behavior
See reproduction section.
Regression?
I never experienced this to work before, but I also never tried it before.
Known Workarounds
Use reflection to call TextEditorSelection.ClearSuggestedX(TextEditor) after setting the CaretPosition.
Impact
I cannot build keyboard navigation between different TextBoxBase controls without the mentioned workaround.
Configuration
.NET 8
Windows 11
x64
I don't think it is specific to any configuration.
Other information
No response
The text was updated successfully, but these errors were encountered:
Description
When you set the CaretPosition property of a RichTextBox programmatically that contains wrapped text, subsequent keyboard movements to the next or previous "line" don't behave correctly.
This is due to the internal TextEditor's _suggestedX member not being cleared correctly (it has the default value 0 and should be double.NaN so that internal keyboard navigations (e.g. by "OnMoveDownByLine(...)") are handled correctly. This is because the GetSuggestedX method requires the current value to be set to double.NaN in order to re-calculate the value correctly.
I wonder if "TextEditorSelection.ClearSuggestedX(TextEditor)" needs to be called automatically whenever the CaretPosition setter is used.
Reproduction Steps
Build a WPF Application (I used .NET 8):
Note that this problem also persists in other scenarios, e.g:
Expected behavior
The caret always behaves correctly, even if the CaretPosition was set programmatically.
Actual behavior
See reproduction section.
Regression?
I never experienced this to work before, but I also never tried it before.
Known Workarounds
Use reflection to call TextEditorSelection.ClearSuggestedX(TextEditor) after setting the CaretPosition.
Impact
I cannot build keyboard navigation between different TextBoxBase controls without the mentioned workaround.
Configuration
.NET 8
Windows 11
x64
I don't think it is specific to any configuration.
Other information
No response
The text was updated successfully, but these errors were encountered: