Static function as an event handler in xaml

I use this code to simulate the functionality of a tab in a silverlight application.

I would really like not to write this function many times, because it needs to be used on a fairly large number of text fields throughout the application. I 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 different places liek this textBox.KeyDown textBox.KeyDown += TabInsert.textBox_KeyDown;

Is there any way I can do this in XAML?

+4
source share
2 answers

You can create a Behavior namespace (System.Windows.Interactivity) to easily connect to text fields that, in the OnAttached () override, subscribe to this event and perform the processing just like you do, and do not subscribe to OnDetaching ().

Sort of:

 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; } } 
+5
source

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

+4
source

Source: https://habr.com/ru/post/1380545/


All Articles