7

Silverlight TextBoxに入力されたタブをキャプチャし、その場所に4つのスペース(またはタブ)をレンダリングするにはどうすればよいですか?

タブナビゲーションをブロックする方法がわかりません。

4

3 に答える 3

8

これが私がすることです(ヨハネスのコードに似ています):

        private const string Tab = "    ";
    void textBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Tab)
        {
            int selectionStart = textBox.SelectionStart;
            textBox.Text = String.Format("{0}{1}{2}", 
                textBox.Text.Substring(0, textBox.SelectionStart),
                Tab,
                textBox.Text.Substring(textBox.SelectionStart + textBox.SelectionLength, (textBox.Text.Length) - (textBox.SelectionStart + textBox.SelectionLength))
                );
            e.Handled = true;
            textBox.SelectionStart = selectionStart + Tab.Length;
        }
    }

これは、テキストを選択して古い「Tab」キーを押しても、期待どおりに動作します。

もう1つ、タブ文字列を「\ t」にしてみましたが、役に立ちませんでした。タブはレンダリングされましたが、1 つのスペースの幅でした。したがって、Tab const の値は 4 つのスペースです。

于 2009-05-08T00:13:57.023 に答える
1

これは私にとってはうまくいくようで、2 番目のイベント ハンドラーやテキスト ボックス名のハード コーディングは必要ありません。

void TabbableTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                e.Handled = true;

                var tb = ((TextBox)sender);
                tb.Text += "\t";
                tb.Select(tb.Text.Length, 0);
            }
        }
于 2010-07-23T22:08:10.250 に答える
1

問題を解決する方法がわかりません。解決策をハックしましたが、うまくいくようです。

KeyDown イベントを以下のように設定します。

expenses.KeyDown += new KeyEventHandler(expenses_KeyDown);

その場合、次のコードを配置します。

void expenses_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                expenses.Text += "    ";
                expenses.Focus();
                expenses.LostFocus += new RoutedEventHandler(expenses_LostFocus);
            }
        }

そして、LostFocus で:

void expenses_LostFocus(object sender, RoutedEventArgs e)
        {
            expenses.Focus();
            expenses.Select(expenses.Text.Length - 1, 0);
        }

LostFocus の最後の行は、編集カーソルをテキストの末尾に設定します。それ以外の場合、フォーカスを取得すると、カーソル位置はテキスト ボックスの先頭になります。

于 2009-05-06T09:23:16.720 に答える