Accessing TableControl from TableControlContent

642
2
08-31-2020 09:20 AM
AlexZlotin1
New Contributor III

Another question about TableControl. I have implemented a custom Attribute Table dockable panel which works exactly like the out-of-the-box one in Pro. The user right-clicks a layer, chooses the option to display the attribute table and a new tab is added to the tab control in my dockable panel (or an existing tab is activated). Now I am trying to add a right-click context menu to each attribute table so the user can zoom to a feature or flash feature. The TableControl community sample describes how to do that. The problem is that in my case the content of each tab is defined via a DataTemplate and in my view model for each tab I have access only to TabControlContent, not TableControl itself. Without TableControl I do not have access to the OID of the feature:

var oid = tableControl.GetObjectIdAsync(tableControl.ActiveRowIndex).Result;

I have attached the relevant files here. I will gladly provide more detail if needed. Thanks!

     

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

You are correct the TableControl, which provides the means to find the select row and the associated object id, is not accessible.  We will have to enhance the API in the future to close that gap.  However, for the time being there is a workaround by searching through the visual elements in order to find the corresponding TableControl.  I attached my sample code, which is using your main components.

To get the sample to work you have to select any layer to be shown in the "Attribute table" dockpane in the TOC of the Contents dockpane:

Then click the "Show Attribute Table" button on the Add-in tab:

Now you should be able to select a row on any table and zoom to the shape's envelope.

Just to highlight how to find the TableControl visual element:

// get the Visual Tree root element from your user control's ViewModel:
public class AttributeTableViewModel : DockPane
{
  ...
  // this should be the visual tree root of our user control
  var visualTreeRoot = this.Content;
}

// using the visual tree root from above we can now traverse the visual tree to find our 'TableControl'
var tableControl = VisualTreeRoot.GetChildOfType<TableControl>();
if (tableControl != null)
{
  // use the table control
}

// and the GetChildOfType<> extension to find a child element of given type in a visual tree
public static T GetChildOfType<T>(this DependencyObject depObj)
    where T : DependencyObject
{
    if (depObj == null) return null;

    for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(depObj); i++)
    {
        var child = System.Windows.Media.VisualTreeHelper.GetChild(depObj, i);
        System.Diagnostics.Debug.WriteLine(child.GetType());
        var result = (child as T) ?? GetChildOfType<T>(child);
        if (result != null) return result;
    }
    return null;
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
AlexZlotin1
New Contributor III

Great workaround! Thanks very much for the help, Wolfgang!

0 Kudos