Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBox CaretIndex property in ViewModel

Is it possible to get/set the value of CaretIndex property of a TextBox control in viewmodel in wpf via Binding defined in view?

Thanks

like image 916
TheITGuy Avatar asked Dec 08 '25 06:12

TheITGuy


1 Answers

The issue here is how to get the CaretIndex of the TextBox control via the view model. If you intent to get it directly by binding to the view model its impossible. As I posted in the previous answer its a CLR property and not a dependency property.

What can we do?

The best solution for that is to follow the steps:

  1. Define attached property on the control via separate class.
  2. Define a property in the view model and bind the attached property to the one in the view-model
  3. Update the control property in the callback of the attached property changed event according to the new value received.

In this case, we still separate the view from the model.

I hope my answer helps you!

like image 108
Ashraf Sayied-Ahmad Avatar answered Dec 09 '25 20:12

Ashraf Sayied-Ahmad