How to use CommandParameter in RelayCommand?

5230
2
Jump to solution
04-13-2021 04:02 AM
TomGeo
by
Occasional Contributor III

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?

- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
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
}
0 Kudos
TomGeo
by
Occasional Contributor III

Thanks Wolf, that was precisely what I needed. 👍

- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
0 Kudos