我使用this代码来模拟我的Silverlight应用程序中的选项卡功能。作为xaml中的事件处理函数的静态函数
我真的很想避免写这个函数很多次,因为它必须在整个应用程序的很多文本框中使用。我创建了一个静态类
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;
}
}
}
,这样我可以从各个地方访问它liek这个textBox.KeyDown += TabInsert.textBox_KeyDown;
有没有一种方法可以让我在XAML做到这一点?
在XAML中,你会做<我:Interaction.Behaviors><地方:TabInsertBehavior /> I :Interaction.Behaviors> –
我绝对赞同xyzzer - 附加的行为是一个很好的解决方法。 (另见[如何在代码中附加行为](http://geekswithblogs.net/SilverBlog/archive/2009/09/22/behaviors-how-to-attach-behavior-in-code-behind-silverlight-3 .aspx)) – DmitryG
我同意,但这只会建立一个非常讨厌的XAML,至少我想有,因为有很多文本框需要此功能... –