如何定义 TAB 在 XAML 文本框中跳转的空格数? [英] How can I define how many spaces a TAB jumps in a XAML TextBox?

查看:23
本文介绍了如何定义 TAB 在 XAML 文本框中跳转的空格数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在此文本框中按下 tab 时,光标会跳过 8 个空格.

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

我怎样才能改变它,让它只跳 4 或 2?

<TextBox
    Width="200"
    Height="200"
    Margin="0 0 10 0"
    AcceptsReturn="True"
    AcceptsTab="True"
    Text="{Binding OutlineText}"/>

推荐答案

您可以创建自己的 TextBox 控件以提供所需的效果:

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;
        }
    }
}

然后您只需在您的 xaml 中使用以下内容:

Then you just use the following in your xaml:

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

请参阅以下原始答案:http://social.msdn.microsoft.com/Forums/en/wpf/thread/0d267009-5480-4314-8929-d4f8d8687cfd

这篇关于如何定义 TAB 在 XAML 文本框中跳转的空格数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆