How to bind SelectionStart Property of Text Box?

后端 未结 4 1130
北恋
北恋 2020-12-06 18:20

I use this:



but it always shows 0.<

4条回答
  •  臣服心动
    2020-12-06 18:54

    This could be an alternate solution:

    View:

    
        
           
               
           
        
    
    

    ViewModel:

        #region TextBoxSelectionChangedCommand
        RelayCommand _TextBoxSelectionChangedCommand = null;
        public ICommand TextBoxSelectionChangedCommand {
            get {
                if (_TextBoxSelectionChangedCommand == null) {
                    _TextBoxSelectionChangedCommand = new RelayCommand((r) => TextBoxSelectionChanged(r), (r) => true);
                }
    
                return _TextBoxSelectionChangedCommand;
            }
        }
    
        protected virtual void TextBoxSelectionChanged(RoutedEventArgs _args) {
            YourCursorPositionVariable = (_args.OriginalSource as System.Windows.Controls.TextBox).SelectionStart;
        }
        #endregion
    

    I agree you has to cast TextBox component type in ViewModel and it's a kind of coupling, but create a custom component will impose to bind on a specific property as well.

提交回复
热议问题