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