Changing the WPF Textbox's blinking cursor style to "Office 2019" style?

رضا جافری 1,291 Reputation points
2022-05-20T23:27:47.933+00:00

The blink cursor can be seen in Word 2019 and 2010:

If you look closely, you'll see that the cursor in Word 2019 progressively fades and returns.
Also when you press the Enter key, the cursor advances slowly to the next line.

Word 2010:
204246-word-2010.gif
Word 2019:
204198-word-2019.gif
How do I use this Office 2019 style in the WPF text box?
Please show me the codes with the output (result).
Thanks

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
764 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 38,251 Reputation points Microsoft Vendor
    2022-05-23T02:13:24.607+00:00

    You give the textbox a logical focus via FocusManager, it also need to be given a keyboard focus for receiving keyboard input. Hence, you could see the cursor. Typically, an element that has keyboard focus will also have logical focus, so you just need to give the textbox a keyboard focus in code behind by using the Focus method on the keyboard class.

    <Grid>  
            <TextBox x:Name="tb"  Height="230" Padding="4"  TextWrapping="Wrap" AcceptsReturn="True"  Width="320" Focusable="True" Loaded="tb_Loaded"  />  
      
        </Grid>  
    
     private void tb_Loaded(object sender, RoutedEventArgs e)  
        {  
          Keyboard.Focus(tb);  
        }  
    

    Furthermore, you may want to read the two main concepts that pertain to focus: keyboard focus and logical focus from this MSDN site:
    https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/focus-overview?redirectedfrom=MSDN&view=netframeworkdesktop-4.8

    Update:

    WPF basically respects Windows' blink rate control panel settings. So does Word 2019 on my system.
    If you want to change the speed of the textbox cursor,you could disable the built-in caret (text cursor) and draw it yourself.

    Add frames as needed and use KeyTime for faster/slower pulses. If you want hard blinks instead of pulses, use <DiscreteColorKeyFrame> instead of <EasingColorKeyFrame> .
    MainWindow.xaml:

    206892-r.txt

    Then you need to manually update the drawing position using the code behind.
    MainWindow.xaml.cs:

      public MainWindow()  
        {  
          InitializeComponent();  
          BookName_TextBox.LayoutUpdated += (sender, e) => {  
            var rect = BookName_TextBox.GetRectFromCharacterIndex(BookName_TextBox.CaretIndex);  
            if (rect.IsEmpty) return;  
            Canvas.SetLeft(Caret, rect.Left);  
            Canvas.SetTop(Caret, rect.Top);  
            Caret.Height = rect.Height;  
          };  
        }  
        private void BookName_TextBox_GotFocus(object sender, RoutedEventArgs e)  
        {  
          Caret.Visibility = Visibility.Visible;  
        }  
      
        private void BookName_TextBox_LostFocus(object sender, RoutedEventArgs e)  
        {  
          Caret.Visibility = Visibility.Collapsed;  
        }  
    

    The result:
    206835-55.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Peter P 6 Reputation points
    2022-05-29T13:10:17.477+00:00

    The caret is a windows call that tells windows to invert the colors on a form somewhere (You basically tell where you want to display the caret).
    If you want to change how this works you basically need to intercept this call or hide the caret and draw your own.

    Word 2010 is basically using the standard one and 2019 is probably using special drawing routines as it has to calculate the position, size and formatting of the characters anyway.

    I've always thought this is a terrible design, the cursor should act and behave the same on any windows. Acting differently can annoy users.
    Word 2019 feels sluggish because of this.

    0 comments No comments