0

I have a custom Button as follows:

<UserControl...>
    <Button HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="3*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Viewbox Stretch="Uniform" Grid.Column="0">
                <TextBlock x:Name="numCopyTB" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=Text}"/>
            </Viewbox>
            <Viewbox Grid.Column="1">
                <TextBlock Style="{StaticResource updownBlock}"/>
            </Viewbox>

        </Grid>
    </Button>
</UserControl>

in its code behind, I added its Text Property. Code behind is below:

public partial class UpdownButton : UserControl
{

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(UpdownButton));

    public UpdownButton()
    {
        InitializeComponent();
    }

    public string Text
    {
        get { return GetValue(TextProperty) as string; }
        set { SetValue(TextProperty, value); }
    }
}

I am learning about CommandBinding and would like to be able to do something like <local:MyCustomButton Command="{Binding MethodName}"/>.

It seems that I need to add a Command Property in codebehind, but what Type should Command be? I did some search and there seems to be many possibilities: RoutedUICommand, RoutedCommand, CommandBinding, DelegateCommand etc., and I am quite lost. Any help is appreciated!

1 Answer 1

1

Any object you bind to the Command DependencyProperty must implement the ICommand interface. My suggestion, if all you're trying to do is use a command to capture and handle click input to your button, would be to use an implementation of the RelayCommand design. This would allow you to create an instance of the RelayCommand in your ViewModel:

public RelayCommand yourCustomButtonCommand { get; set; }

And in your constructor, instantiate the RelayCommand like so:

MyCommand = new RelayCommand( 
ExecuteMyCommand, 
() => _canExecuteMyCommand); 

where ExecuteMyCommand is the method you wish to be executed each time the Button is clicked, and _canExecuteMyCommand is a Predicate that determines whether the button should be clickable or not at any given point in time.

Based on your question, here's how you could inherit from Button in your code-behind:

public partial class UpdownButton : Button
{
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(UpdownButton));

    public UpdownButton()
    {
        InitializeComponent();
    }

    public string Text
    {
        get { return GetValue(TextProperty) as string; }
        set { SetValue(TextProperty, value); }
    }
}

This would produce an object that inherits all dependency properties from Button and has the new ones you've defined above. The most important thing to remember is that the top-level type for your XAML in the user control has to be changed to <Button>, not <UserControl>.

With these changes, your UpdownButton will now have the Command property, as well as every other property Button has.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks for the reply. if i were to use RelayCommand, i would still need to do something like <local:MyCustomButton Command="{Binding RelayCommandName}"/> to "bind" the command to the button, right? The problem is that currently MyCustomButton does not have the Command property (doesn't show up in intellisense). so is it necessary to implement the ICommand interface in order to use RelayCommand?
@green Hm - are you inheriting from Button in your custom Button? My understanding is that in inheriting from a control you inherit all the dependency properties.
@green Hm, this is probably ignorance on my part, but if you're trying to make something that works almost like a Button, but a little different, why not just inherit from Button?
probably because of my ignorance on WPF, lol. i just searched and saw some examples in C#; is it possible to inherit from Button in XAML? it's easier to define grid and other structures in XAML.
never mind, just saw this post
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.