Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static function as eventhandler in xaml

I am using this code to simulate the tab functionality in my silverlight application.

I would really like to avoid writing the function a lot of times because it has to be used on quite a lot of textboxes throughout the application. I have created a static class

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

so that I can access it from various places liek this textBox.KeyDown += TabInsert.textBox_KeyDown;

Is there a way I can do this in XAML?

like image 669
Ivan Crojach Karačić Avatar asked Nov 10 '11 09:11

Ivan Crojach Karačić


2 Answers

Unfortunately, there is no direct way to do this in XAML.The event handlers you write in the code behind must be instance methods and cannot be static methods. These methods must be defined by the partial class within the CLR namespace identified by x:Class. You cannot qualify the name of an event handler to instruct a XAML processor to look for an event handler for event wiring in a different class scope.

like image 106
DmitryG Avatar answered Oct 13 '22 00:10

DmitryG


You can create a Behavior (System.Windows.Interactivity namespace) to easily attach to textboxes that in the OnAttached() override subscribes to the event and does the handling as you do and unsubscribes in OnDetaching().

Something like:

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;
    }
}
like image 31
Filip Skakun Avatar answered Oct 12 '22 22:10

Filip Skakun