Ich hatte eine Cursorposition Eigenschaft in meinem Viewmodel, die die Position des Cursors in Textfeld in der Ansicht entscheidet. Wie kann ich die cursorposition -Eigenschaft an die tatsächliche Position des Cursors innerhalb des Textfelds binden.Handhabung Cursor in Textfeld
0
A
Antwort
1
Ich fürchte, Sie können nicht ... zumindest nicht direkt, da es keine "CursorPosition" -Eigenschaft auf das TextBox-Steuerelement gibt.
Sie können dieses Problem umgehen, indem Sie in Code-Behind eine DependencyProperty erstellen, die an das ViewModel gebunden ist und die Cursorposition manuell verarbeitet. Hier ein Beispiel:
/// <summary>
/// Interaction logic for TestCaret.xaml
/// </summary>
public partial class TestCaret : Window
{
public TestCaret()
{
InitializeComponent();
Binding bnd = new Binding("CursorPosition");
bnd.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(this, CursorPositionProperty, bnd);
this.DataContext = new TestCaretViewModel();
}
public int CursorPosition
{
get { return (int)GetValue(CursorPositionProperty); }
set { SetValue(CursorPositionProperty, value); }
}
// Using a DependencyProperty as the backing store for CursorPosition. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CursorPositionProperty =
DependencyProperty.Register(
"CursorPosition",
typeof(int),
typeof(TestCaret),
new UIPropertyMetadata(
0,
(o, e) =>
{
if (e.NewValue != e.OldValue)
{
TestCaret t = (TestCaret)o;
t.textBox1.CaretIndex = (int)e.NewValue;
}
}));
private void textBox1_SelectionChanged(object sender, RoutedEventArgs e)
{
this.SetValue(CursorPositionProperty, textBox1.CaretIndex);
}
}
0
Sie können die CaretIndex-Eigenschaft verwenden. Es ist jedoch keine DependencyProperty und scheint INotifyPropertyChanged nicht zu implementieren, sodass Sie nicht wirklich daran binden können.
Verwandte Themen
- 1. Textfeld manchmal Cursor fehlt
- 2. Blinken Cursor im Textfeld verhindern
- 3. Cursor-Position in einem Textfeld eingeben
- 4. Kopieren/Einfügen Cursor Sprache in Textfeld
- 5. Cursor Fokus auf Textfeld in WPF/C#
- 6. iPhone Tastatur Handhabung mit benutzerdefinierten Textfeld?
- 7. Wie UITextField Cursor zum nächsten Textfeld verschieben
- 8. Ändern Sie die Cursor-Position in einem Textfeld mit React
- 9. einfügen Wert in den letzten Textfeld, wo Cursor
- 10. Cursor-Position in einem Textfeld (Zeichenindex, nicht x/y-Koordinaten)
- 11. Setzen Sie die Cursor/Cursor-Position an das Ende des String-Wertes WPF Textfeld
- 12. Rücktaste der iPhone-Tastatur bewegt den Cursor zum nächsten Textfeld
- 13. Handhabung Datumsformat in schnellen
- 14. Handhabung Negation in Worte
- 15. Handhabung richtig in wit.ai
- 16. Signal in C Handhabung
- 17. Handhabung Deklination in iOS
- 18. Handhabung onNewIntent in Fragment
- 19. Handhabung Downloads in QtWebEngine
- 20. Handhabung \ n in LineBreakMeasurer
- 21. Handhabung Unendlichkeiten in Dygraph
- 22. Handhabung let in Hypothese
- 23. Handhabung abbrechen in GKMatchmakerviewcontroller
- 24. Handhabung Zähler in Textview
- 25. Nicht editierbares Textfeld in wxPython
- 26. Handhabung der ENTER-Taste in TextBox, ASP.NET
- 27. UITextField gesetzt Cursor Textposition
- 28. Setzen Sie den Cursor auf eine bestimmte Position in einer bestimmten Zeile in einem Textfeld
- 29. Lernereignis Handhabung
- 30. Capturing TAB-Taste in Textfeld
Danke für die Antwort Thomas. Ich werde es ausprobieren und werde zu dir zurückkommen. – deepak