Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

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

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 only 4 or 2?

<TextBox
    Width="200"
    Height="200"
    Margin="0 0 10 0"
    AcceptsReturn="True"
    AcceptsTab="True"
    Text="{Binding OutlineText}"/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

<cc:MyTextBox AcceptsReturn="True" TabSize="10" x:Name="textBox"/>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...