2011-11-10 3 views
4

Silverlightアプリケーションのタブ機能をシミュレートするのに、thisコードを使用しています。xamlのイベントハンドラとしての静的関数

アプリケーション全体で非常に多くのテキストボックスで使用する必要があるため、この関数を頻繁に書くのは避けたいと思います。私はこのtextBox.KeyDown += TabInsert.textBox_KeyDown;

liekさまざまな場所からアクセスできるように、私は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; 
     } 
    } 
} 

を静的クラスを作成していますか?

答えて

5

OnAttached()オーバーライドでイベントを購読し、OnDetaching()でやりとりするように処理したり、取り消したりするテキストボックスに簡単にアタッチするためのビヘイビア(System.Windows.Interactivity名前空間)を作成できます。

ような何か:

public class TabInsertBehavior : Behavior<TextBox> 
{ 
    /// <summary> 
    /// Called after the behavior is attached to an AssociatedObject. 
    /// </summary> 
    /// <remarks> 
    /// Override this to hook up functionality to the AssociatedObject. 
    /// </remarks> 
    protected override void OnAttached() 
    { 
     base.OnAttached(); 
     this.AssociatedObject.KeyDown += textBox_KeyDown; 
    } 

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

    /// <summary> 
    /// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred. 
    /// </summary> 
    /// <remarks> 
    /// Override this to unhook functionality from the AssociatedObject. 
    /// </remarks> 
    protected override void OnDetaching() 
    { 
     base.OnDetaching(); 
     this.AssociatedObject.KeyDown -= textBox_KeyDown; 
    } 
} 
+2

<ローカル:TabInsertBehavior />

+0

私は絶対にxyzzerを試してみました。 (「コードビハインドでビヘイビアをアタッチする方法」も参照してください(http://geekswithblogs.net/SilverBlog/archive/2009/09/22/behaviors-how-to-attach-behavior-in-code-behind-silverlight-3 .aspx)) – DmitryG

+0

私も同意しますが、これはちょうど厄介なXAMLを構築するだけです。この機能を必要とするテキストボックスがたくさんあるので、少なくとも私は持っていたいと思っています... –

4

残念ながら、there is no direct way to do this in XAML。コードビハインドで記述するイベントハンドラはインスタンスメソッドでなければならず、静的メソッドであってはなりません。これらのメソッドは、x:Classによって識別されるCLR名前空間内の部分クラスによって定義される必要があります。イベントハンドラの名前を修飾して、XAMLプロセッサに異なるクラススコープでのイベント配線のイベントハンドラを探すように指示することはできません。

+0

おかげで...そして、コードが背後にそれがあるべき...あなたはどうしたらあなたのXAMLで –

関連する問題