WPF command binding for custom UserControl

跟風遠走 提交于 2020-01-06 07:00:12

问题


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:


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.



来源:https://stackoverflow.com/questions/26686248/wpf-command-binding-for-custom-usercontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!