How can I define how many spaces a TAB jumps in a XAML TextBox?

前端 未结 4 573
面向向阳花
面向向阳花 2020-12-31 10:26

When the user presses a tab in this textbox, the cursor jumps an equivalent of 8 spaces.

How can I change it so it jumps on

4条回答
  •  庸人自扰
    2020-12-31 10:42

    You can create your own TextBox control to give the desired affect:

    public class MyTextBox : TextBox
    {
        public MyTextBox()
        {
            //Defaults to 4
            TabSize = 4;
        }
    
        public int TabSize
        {
            get;
            set;
        }
    
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                String tab = new String(' ', TabSize);
                int caretPosition = base.CaretIndex;
                base.Text = base.Text.Insert(caretPosition, tab);
                base.CaretIndex = caretPosition + TabSize + 1;
                e.Handled = true;
            }
        }
    }
    

    Then you just use the following in your xaml:

    
    

    See the following original answer: http://social.msdn.microsoft.com/Forums/en/wpf/thread/0d267009-5480-4314-8929-d4f8d8687cfd

提交回复
热议问题