Select to view content in your preferred language

How to detect a left mouse click on a row in a TableControl via ArcGIS Pro SDK

1141
4
Jump to solution
12-30-2020 01:11 PM
Amadeus111
Frequent Contributor

I have utilized a TableControl and it works fine.

 

<editing:TableControl AutomationProperties.AutomationId="_tableControl" x:Name="tableControl" 
     Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
     SelectedRowsChanged="tableControl_SelectedRowsChanged"
     TableContent="{Binding Path=TableContent}" 
     RowContextMenu="{StaticResource MyRowContextMenu}"
     SelectedRowContextMenu="{StaticResource MyRowContextMenu}">
</editing:TableControl>

 

Is there way to detect user left mouse click on a row in the table? I tried PreviewMouseLeftButton but that event is valid for whole table not specifically for a row. 

1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I will make the developer of this control aware of this issue.  However, there is a solution to get the selected row for a TableControl by using the ActiveRowIndex attribute of the TableControl.  In order to illustrate this i update this sample: Map-Exploration - TableControlsDockpane sample 

I updated the XAML in Map-Exploration\TableControlsDockpane\AttributeTable.xaml by adding the ActiveRowIndex attribute to my TableControl:

<TabControl.ContentTemplate>
 <DataTemplate>
  <editing:TableControl TableContent="{Binding TableContent}"
                        RowContextMenu="{Binding RowContextMenu}"
                        ActiveRowIndex="{Binding ActiveRowIdx, Mode=TwoWay}" 
                        SelectedRowContextMenu="{Binding RowContextMenu}" />
 </DataTemplate>
</TabControl.ContentTemplate>

And then i added the following code snippet to Map-Exploration\TableControlsDockpane\TabItemViewModel.cs:

private int _lastRowIdx = -1;
private int _ActiveRowIdx;
public int ActiveRowIdx
{
	get { return _ActiveRowIdx; }
	set
	{
		_ActiveRowIdx = value;
		NotifyPropertyChanged(nameof(ActiveRowIdx));
		var tableControl = VisualTreeRoot.GetChildOfType<TableControl>();
		if (tableControl != null)
		{
			var rowIdx = tableControl.ActiveRowIndex;
			if (rowIdx == _lastRowIdx) return;
			_lastRowIdx = rowIdx;
			System.Diagnostics.Debug.WriteLine($@"row: {rowIdx}");
			_ = GetObjectIdAsync(tableControl, rowIdx);
		}
	}
}

public async Task<long> GetObjectIdAsync (TableControl tableControl, int rowIdx)
{
	var oid = await tableControl.GetObjectIdAsync(rowIdx);
	ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show ($@"rowIdx:  {rowIdx} has OID: {oid}");
	return oid;
}

 The trick is to add "Mode=TwoWay" to the ActiveRowIndex attribute in XAML and that ActiveRowIndex is also set when the row is unselected.  Running the code is get this:

AttributeTable.png

 

View solution in original post

4 Replies
KirkKuykendall1
Frequent Contributor

It would be nice if Esri added ItemContainerStyle to TableControl so something like this could be done.

Amadeus111
Frequent Contributor

My original idea was using DataGrid but I have about 50K rows. In my case, TableControl is better to handle that many rows in addition coming with some nice ESRI controls. However, I could not find any event such as  row click or mouse left  click on row. I don't know how to implement one either.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I will make the developer of this control aware of this issue.  However, there is a solution to get the selected row for a TableControl by using the ActiveRowIndex attribute of the TableControl.  In order to illustrate this i update this sample: Map-Exploration - TableControlsDockpane sample 

I updated the XAML in Map-Exploration\TableControlsDockpane\AttributeTable.xaml by adding the ActiveRowIndex attribute to my TableControl:

<TabControl.ContentTemplate>
 <DataTemplate>
  <editing:TableControl TableContent="{Binding TableContent}"
                        RowContextMenu="{Binding RowContextMenu}"
                        ActiveRowIndex="{Binding ActiveRowIdx, Mode=TwoWay}" 
                        SelectedRowContextMenu="{Binding RowContextMenu}" />
 </DataTemplate>
</TabControl.ContentTemplate>

And then i added the following code snippet to Map-Exploration\TableControlsDockpane\TabItemViewModel.cs:

private int _lastRowIdx = -1;
private int _ActiveRowIdx;
public int ActiveRowIdx
{
	get { return _ActiveRowIdx; }
	set
	{
		_ActiveRowIdx = value;
		NotifyPropertyChanged(nameof(ActiveRowIdx));
		var tableControl = VisualTreeRoot.GetChildOfType<TableControl>();
		if (tableControl != null)
		{
			var rowIdx = tableControl.ActiveRowIndex;
			if (rowIdx == _lastRowIdx) return;
			_lastRowIdx = rowIdx;
			System.Diagnostics.Debug.WriteLine($@"row: {rowIdx}");
			_ = GetObjectIdAsync(tableControl, rowIdx);
		}
	}
}

public async Task<long> GetObjectIdAsync (TableControl tableControl, int rowIdx)
{
	var oid = await tableControl.GetObjectIdAsync(rowIdx);
	ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show ($@"rowIdx:  {rowIdx} has OID: {oid}");
	return oid;
}

 The trick is to add "Mode=TwoWay" to the ActiveRowIndex attribute in XAML and that ActiveRowIndex is also set when the row is unselected.  Running the code is get this:

AttributeTable.png

 

Amadeus111
Frequent Contributor

Thanks a lot for detailed explanation. I did not think about two way binding. Let me try it.

0 Kudos