简体   繁体   中英

Is it better to wire up multiple items to an event handler in custom control class, or use Generic.xaml code-behind?

I am creating a numeric keypad as a custom WPF control. I am building the custom control as a class library which will be included in other applications. The keypad has multiple buttons I need to wire up to a single event handler. The event handler will determine which button sent the event and then respond appropriately. The custom control template is in the generic.xaml file.

I see two ways of doing this and would like to know which is seen as the better solution.

Approach 1:

I can override the OnApplyTemplate method in my custom control class, find each button, and wire them each up to the event handler:

        Button enterButton = Template.FindName("PART_EnterButton, this") as Button;
        Button cancelButton = Template.FindName("PART_CancelButton", this) as Button;
        Button deleteButton = Template.FindName("PART_DeleteButton", this) as Button;
        Button decimalButton = Template.FindName("PART_DecimalButton", this) as Button;
        Button buttonOne = Template.FindName("PART_ButtonOne", this) as Button;
        Button buttonTwo = Template.FindName("PART_ButtonTwo", this) as Button;
        Button buttonThree = Template.FindName("PART_ButtonThree", this) as Button;
        Button buttonFour = Template.FindName("PART_ButtonFour", this) as Button;
        Button buttonFive = Template.FindName("PART_ButtonFive", this) as Button;
        Button buttonSix = Template.FindName("PART_ButtonSix", this) as Button;
        Button buttonSeven = Template.FindName("PART_ButtonSeven", this) as Button;
        Button buttonEight = Template.FindName("PART_ButtonEight", this) as Button;
        Button buttonNine = Template.FindName("PART_ButtonNine", this) as Button;

        enterButton.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);
        cancelButton.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);
        deleteButton.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);
        decimalButton.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);
        buttonOne.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);
        buttonTwo.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);
        buttonThree.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);
        buttonFour.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);
        buttonFive.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);
        buttonSix.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);
        buttonSeven.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);
        buttonEight.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);
        buttonNine.Click += new RoutedEventHandler(KeyPadButtonPressEventHandler);

Pros:

  • This keeps all of my logic in one place, my class definition file.

Cons:

  • This approach requires many more lines of code than the second approach.
  • Potential performance hit when the control loads for the first time.

Approach 2:

I can create a code-behind for my generic.xaml file. This will enable me to wire up the event handlers in the xaml, as the generic.xaml file now knows where to find the event handler:

<Button x:Name="buttonThree" Style="{StaticResource ResourceKey=Buttons}"
            Click="KeyPadButtonPressEventHandler" Content="3"  />

And in the Generic.xaml.cs file:

namespace KeyPad
{
partial class Generic : ResourceDictionary
{
    private void KeyPadButtonPressEventHandler(object sender, RoutedEventArgs e)
    {
        //Event handler logic here
    }
}
}

Pros:

  • No lines of code needed to wire up the buttons to the event handler.
  • Potential performance savings as I don't need to wire up all the buttons when the control loads.

Cons:

  • I have introduced an additional file to my assembly, another place a developer would have to look to find my logic.

I'd keep any custom Control logic inside the Control itself.

Remember the generic.xaml is used only to define the look of the Control which makes it easier to theme.

The style you define in the generic.xaml could easily be overridden.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM