views:

329

answers:

1

I've got a UserControl that has 3 buttons in it, Add Edit and Delete. Nothing crazy here.

On my parent UserControl, I insert my buttons UserControl. They show up as expected. But I want to bind Commands to each of the buttons from my Parent UserControl. How do I access each of the buttons' paremeters from the Parent UserControl?

+3  A: 

The cleanest way to do this is to expose dependency properties on your UserControl that you wish to manipulate from the control hosting the UserControl:

public class UserControl
{
    public ICommand AddCommand
    {
        ...
    }

    ...
}

The XAML in your UserControl can then bind to these properties:

<UserControl>
    <Button Command="{Binding AddCommand}">Add</Button>
    ...
</UserControl>

And, of course, your host can then use those properties:

<Window>
    <local:YourUserControl AddCommand="{Binding MyAddCommand}"/>
</Window>

HTH, Kent

Kent Boogaart