Click here to Skip to main content
15,881,819 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, I have the custom input field using MVVM model, but the issue is that the caret is not working with the mouse though it works with the keyboard. but that usually works for the plain text block.

Custom Text Block

What I have tried:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace Controls.Behaviors
{
  public class MoveCaretBehavior : Behavior<UIElement>
  {

    public static readonly DependencyProperty KeyboardTextBoxProperty = DependencyProperty.RegisterAttached(
        "KeyboardTextBox",
        typeof(TextBox),
        typeof(MoveCaretBehavior));

  
    public TextBox KeyboardTextBox
    {
      get
      {
        return (TextBox)GetValue(KeyboardTextBoxProperty);
      }

      set
      {
        SetCurrentValue(KeyboardTextBoxProperty, value);
      }
    }

    protected override void OnAttached()
    {
      base.OnAttached();

      KeyboardTextBox = AssociatedObject as AlphanumericInputField;

      if (KeyboardTextBox == null)
      {
        return;
      }
      KeyboardTextBox.GotFocus += TextBoxGotFocus;
    }

    protected override void OnDetaching()
    {
      if (KeyboardTextBox == null)
      {
        return;
      }
      KeyboardTextBox.GotFocus -= TextBoxGotFocus;

      base.OnDetaching();
    }

    private void TextBoxGotFocus(object sender, RoutedEventArgs routedEventArgs)
    {
      KeyboardTextBox.CaretIndex = KeyboardTextBox.Text.Length;
    }
  }
}
Posted
Updated 16-May-21 19:28pm
v2
Comments
[no name] 14-May-21 14:31pm    
Should have stuck with a "standard" text box and explained what you needed. Can't tell what the point of your "custom" text box / block is (you're using both terms) since they accept "alphanumeric" by default.
Manav Mehta 14-May-21 14:50pm    
I want that text caret to move for alphanumeric ones... so I should change the behavior of textbox to alphanumeric ones that what I should do?
[no name] 14-May-21 17:20pm    
The WPF TextBox accepts any character; it has a carat / cursor that works. If you want to limit characters, then hook up PreviewKeyDown, and filter (e.Key) using VirtualKey(s) (between Number0-9; A-Z)
Manav Mehta 15-May-21 0:23am    
My issue is that the carat is not working with the mouse in my alphanumeric control.

1 solution

In my experience, when the caret gets out of sync with the text it tends to stay out of sync. It is configuring the caret in the Xaml that seems to set it off on the wrong track. Waiting until the window has loaded before setting the TextBox focus or positioning the caret in the code behind usually gives more consistent results.


C#
private void OnGotFocus(object sender, RoutedEventArgs e)
 {
     var textBox = sender as TextBox;
     if (textBox == null) return;
     //Place the caret at end of any text
     textBox.SelectionStart = textBox.Text.Length;
     textBox.SelectionLength = 0;

 }

 private void OnLoaded(object sender, RoutedEventArgs e)
 {
     //set the focus on the first textBox
      ForeNameTB.Focus();
 }
 
Share this answer
 
v2
Comments
Manav Mehta 18-May-21 15:05pm    
Still unable to move mouse in text box
George Swan 19-May-21 2:10am    
I am sorry that the solution has not helped you. I hope that it may be of use to other members. Best wishes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900