【发布时间】:2011-11-10 09:42:40
【问题描述】:
我正在使用this 代码来模拟我的silverlight 应用程序中的选项卡功能。
我真的很想避免多次编写该函数,因为它必须在整个应用程序中的大量文本框中使用。我创建了一个静态类
public static class TabInsert
{
private const string Tab = " ";
public static void textBox_KeyDown(object sender, KeyEventArgs e)
{
TextBox textBox = sender as TextBox;
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;
}
}
}
这样我就可以从各个地方访问它,就像textBox.KeyDown += TabInsert.textBox_KeyDown;
有没有办法在 XAML 中做到这一点?
【问题讨论】:
标签: c# xaml silverlight-4.0 event-handling