Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting the CaretPosition in a RichTextBox Control does not correctly initialize the internal TextEditor's state #10151

Open
digital-ember opened this issue Dec 6, 2024 · 0 comments
Labels
Investigate Requires further investigation by the WPF team.

Comments

@digital-ember
Copy link

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):

<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);
		}
	}
}
  1. Click the Button
  2. 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:

  1. Restart the application
  2. Click the button
  3. Press the ArrowKey Down next and observe that the caret is placed at position 0 instead of 100
  4. Move the Caret left or right with the arrow keys (but stay in the second line)
  5. Click the button again
  6. Press the ArrowKey Down abd observe that the cache seems to have been cleared correctly for this movement
  7. 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
  8. Click the button again
  9. 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

@himgoyalmicro himgoyalmicro added the Investigate Requires further investigation by the WPF team. label Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Investigate Requires further investigation by the WPF team.
Projects
None yet
Development

No branches or pull requests

2 participants