Martyn Ball
Martyn Ball

Reputation: 4889

Adding functionality to custom control

I need some advice on how to add functionality to my custom control, all it is, is a textbox and 2 buttons which increase and decrease it's value, which will be numeric.

What I don't understand is the best way to add the code behind for this functionality. Should I use commands in my custom control code behind? Here is the code I have at the moment:

XAML - Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Controls">

    <Style TargetType="{x:Type local:NumericUpDown}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:NumericUpDown}">
                    <Grid HorizontalAlignment="Center" Margin="5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>

                        <Button Grid.Column="0" 
                                Padding="5"
                                Background="Gray"
                                Command="{Binding Source=NumBox, Path=Increase}">
                            <Button.Content>
                                <Path Data="M0,0 L1,0 0.5,1Z" 
                                      Width="6" 
                                      Height="6" 
                                      Fill="White"
                                      Stretch="Fill"/>
                            </Button.Content>
                        </Button>
                        <TextBox x:Name="NumBox" 
                                 Grid.Column="1" 
                                 Text="0" 
                                 Padding="2" />
                        <Button Grid.Column="2" 
                                Padding="5"
                                Background="Gray">
                            <Button.Content>
                                <Path Data="M0,1 L1,1 0.5,0Z" 
                                      Width="6" 
                                      Height="6" 
                                      Fill="White" 
                                      Stretch="Fill" />
                            </Button.Content>
                        </Button>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

NumericUpDown.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Controls
{
    public class NumericUpDown : Control
    {
        static NumericUpDown()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), 
                new FrameworkPropertyMetadata(typeof(NumericUpDown)));
        }
    }
    public static class Command
    {
        public static readonly RoutedUICommand Increase = new RoutedUICommand("Increase the number", "IncreaseCommand", typeof(Command));
    }

    public class IncreaseCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return false;
        }
        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            MessageBox.Show("click!");
        }
    }
}

Clearly I haven't got the command working just yet, decided to ask for advice before I continue.

Upvotes: 1

Views: 100

Answers (2)

christoph
christoph

Reputation: 1051

Mostly when coming across CustomControls you override the OnApplyTemplate() method like this. (Remember naming the buttons)

public class NumericUpDown : Control
{
    static NumericUpDown()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), 
            new FrameworkPropertyMetadata(typeof(NumericUpDown)));
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        Button increaseButton = this.GetTemplateChild("increaseButton") as Button;
        if (increaseButton != null)
        {
            increaseButton.Click += increaseButton_Click;
        }
    }

    private void increaseButton_Click(object sender, RoutedEventArgs e)
    {
        // Do what you want to do.
    }
}

Upvotes: 1

Eli Dagan
Eli Dagan

Reputation: 848

It's depend on what you want to do when the user click on the buttons. If the functionality is only to increase and decrease the value in the text box. you can register to the command click event on the code behind (it is your logic and you don't want to add more dynamic logic).

If you also want to give the user an opportunity to add his custom functionality when he click on the button you can also also add command

Upvotes: 1

Related Questions