WPF Event Handler in Another Class
- by Nathan Tornquist
I have built a series of event handlers for some custom WPF controls. The event handles format the text displayed when the user enters or leaves a textbox based on the type of data contained (Phone number, zip code, monetary value, etc.)
Right now I have all of the events locally in the C# code directly attached to the xaml. Because I have developed a could controls, this means that the logic is repeated a lot, and if I want to change the program-wide functionality I would have to make changes everywhere the event code is located.
I am sure there is a way to put all of my event handlers in a single class. Can anyone help point me in the correct direction?
I saw this article: Event Handler located in different class than MainWindow But I'm not sure if it directly relates to what I'm doing. I would rather make small changes to the existing logic that I have, as it works, then rewrite everything into commands.
I would essentially like to something like this if possible:
LostFocus="ExpandedTextBoxEvents.TextBox_LostFocus"
It is easy enough to do something like this:
private void TextBoxCurrencyGotFocus(object sender, RoutedEventArgs e)
{
ExpandedTextBoxEvents.TextBoxCurrencyGotFocus(sender, e);
}
private void TextBoxCurrencyLostFocus(object sender, RoutedEventArgs e)
{
ExpandedTextBoxEvents.TextBoxCurrencyLostFocus(sender, e);
}
But that is less elegant.