How to use CommandParameter in RelayCommand?

249
2
Jump to solution
04-13-2021 04:02 AM
ThomasBecker1
Occasional Contributor II

In my UI I have multiple buttons to call the OpenItemDialog and I would like to reuse the method to call the dialoge by using the CommandParameter to differentiate the initiating button.
How can I do this using the RelayCommand?

0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

This community sample shows how to do this:  arcgis-pro-sdk-community-samples/Map-Exploration/TableControlsDockpane at master · Esri/arcgis-pro-s...

In the xaml you can specify the CommandParameter: 

<Button Command="{Binding DataContext.TableCloseCommand, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}"
        CommandParameter="{Binding TableName}" >

TableName in this example is of the type string.

In the code behind you can use the CommandParameter by simple adding 'param' to your RelayCommand as shown here:

public ICommand TableCloseCommand { get; set; }

public AttributeTableViewModel()
{
  TableCloseCommand = new RelayCommand((param) => OnCloseTab(param), () => true);
}

private void OnCloseTab(object param)
{
  // cast the [object] param to the correct type [string]
  string tableName = param.ToString();
  // use your TableName CommandParameter for processing
}

View solution in original post

0 Kudos
2 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

This community sample shows how to do this:  arcgis-pro-sdk-community-samples/Map-Exploration/TableControlsDockpane at master · Esri/arcgis-pro-s...

In the xaml you can specify the CommandParameter: 

<Button Command="{Binding DataContext.TableCloseCommand, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}"
        CommandParameter="{Binding TableName}" >

TableName in this example is of the type string.

In the code behind you can use the CommandParameter by simple adding 'param' to your RelayCommand as shown here:

public ICommand TableCloseCommand { get; set; }

public AttributeTableViewModel()
{
  TableCloseCommand = new RelayCommand((param) => OnCloseTab(param), () => true);
}

private void OnCloseTab(object param)
{
  // cast the [object] param to the correct type [string]
  string tableName = param.ToString();
  // use your TableName CommandParameter for processing
}

View solution in original post

0 Kudos
ThomasBecker1
Occasional Contributor II

Thanks Wolf, that was precisely what I needed. 👍

0 Kudos