Question

I have a UserControl that contains several buttons, and for each button I have a Command DependencyProperty defined. The UserControl XAML is (some elements removed for brevity):

<UserControl>
   <Grid>
      <Button Command="{Binding CloseCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" ToolTip="Delete">
      <Button Command="{Binding NavigateCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" ToolTip="Launch">
   </Grid>
</UserControl>

The DepencyProperties in the UserControl codebehind are:

public static readonly DependencyProperty CloseCommandProperty = DependencyProperty.Register("CloseCommand", typeof (ICommand), typeof (Tile));

public ICommand CloseCommand
{
    get { return (ICommand)GetValue(CloseCommandProperty); }
    set { SetValue(CloseCommandProperty, value); }
}

public static readonly DependencyProperty NavigateCommandProperty = DependencyProperty.Register("NavigateCommand", typeof (ICommand), typeof (Tile));

public ICommand NavigateCommand
{
    get { return (ICommand) GetValue(NavigateCommandProperty); }
    set { SetValue(NavigateCommandProperty, value); }
}

I'm using MVVM Light and I'm able to bind to these commands via RelayCommands on my ViewModel for the View that contains several instances of this UserControl. I'd like to be able to expose individual CommandParameters for each of the Commands on my UserControl. Is this possible?

In future I'd also like the entire UserControl (essentially the grid contained immediately within it) to expose commands for some other events (click, drag and data drop), again, each would expose its own CommandParameters.

I've not found much information about multiple Command and CommandParameter pairing, and so I'm wondering if I've taken the wrong approach. Would a CustomControl be more appropriate? Or would this be best solved using a DataTemplate? Exposing events like click and drag on a grid as commands also doesn't seem to have much coverage - is there a better container control that would suit my needs?

Was it helpful?

Solution

You can create RelayCommand to accept the CommandParameter like this -

closeCommand = new RelayCommand<bool>((flag) => Close(flag));

private void Test(bool flag)
{
    if(flag)
       CloseWindow();
}

i.e. by using the generic RelayCommand<T> implementation;

The Execute method is passed to the RelayCommand’s constructor in form of an Action (or Action if the command has a parameter, in which case you must use the RelayCommand class).

Using RelayCommands in Silverlight 3 and WPF

Ref. : - Proposing a new RelayCommand snippet for MVVM Light V4

and then pass the parameter using CommandParameter property of Button.

<Button Command="{Binding CloseCommand }" 
        CommandParameter="{Binding SomeCommandParameter}" ... />

Or

<Button Command="{Binding CloseCommand }" 
        CommandParameter="True" ... />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top