<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Error populating the fields for the selected layer in a dockpane in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/error-populating-the-fields-for-the-selected-layer/m-p/1269508#M9560</link>
    <description>&lt;P&gt;Hi Thi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;I attached a 2.9 sample that should build and run in 2.8.&amp;nbsp; Remember that when you add data to the UI as a result of an Event (I.E. layer changed events) then very likely your code will run on a non-UI thread.&amp;nbsp; Also anything within QueuedTask.Run is running on a non-UI thread.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 20 Mar 2023 17:33:47 GMT</pubDate>
    <dc:creator>Wolf</dc:creator>
    <dc:date>2023-03-20T17:33:47Z</dc:date>
    <item>
      <title>Error populating the fields for the selected layer in a dockpane</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/error-populating-the-fields-for-the-selected-layer/m-p/1269096#M9552</link>
      <description>&lt;P&gt;Hi Everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not sure why I am unable to populate the selected layer's fields. I have two combo boxes in my dockpane, layers in the map and selected layer fields. It's weird that when I comment out the GetFieldAsync() method, I am able to populate the feature layers on the active map, but when I don't comment the method out, then both the layer combo box and the selected layer's fields combo box does not show. If you can help me troubleshoot this problem, I would appreciate the help. Here is codes below:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;   private FeatureLayer _selectedLayer;

        // Selected feature layer
        public FeatureLayer SelectedLayer
        {
            get { return _selectedLayer; }
            set
            {
                SetProperty(ref _selectedLayer, value, () =&amp;gt; SelectedLayer);
                _ = GetFieldsAsync();

            }
        }

private async Task GetFieldsAsync()
        {
            LayerFields.Clear();
            SelectedField.Clear();
            if (SelectedLayer == null)
                return;

            await QueuedTask.Run((Action)(() =&amp;gt;
            {
                ObservableCollection&amp;lt;string&amp;gt; fields = new ObservableCollection&amp;lt;string&amp;gt;();

                foreach (FieldDescription fd in SelectedLayer?.GetFieldDescriptions())
                {
                    string shapeField = SelectedLayer.GetFeatureClass().GetDefinition().GetShapeField();
                    if (fd.Name == shapeField) continue;        
                    fields.Add(fd.Name);
                }

            }));
        }&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 17 Mar 2023 19:19:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/error-populating-the-fields-for-the-selected-layer/m-p/1269096#M9552</guid>
      <dc:creator>tzz_12</dc:creator>
      <dc:date>2023-03-17T19:19:01Z</dc:date>
    </item>
    <item>
      <title>Re: Error populating the fields for the selected layer in a dockpane</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/error-populating-the-fields-for-the-selected-layer/m-p/1269139#M9554</link>
      <description>&lt;P&gt;You are updating a list/combobox from a background thread, hence you have to take certain precautions.&amp;nbsp; Like using:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;System.Windows.Data.BindingOperations.EnableCollectionSynchronization(_layers, _lock);
            &lt;/LI-CODE&gt;&lt;P&gt;This enables your observable collection for updates on non GUI threads.&amp;nbsp; Also once you define and 'lock' an observable collection you can't assign a new collection (like you did with your field names).&amp;nbsp; &amp;nbsp; I attached a sample that shows FeatureLayers and field names on a Dockpane.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Wolf_0-1679087390501.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/65607iB60EA8F679D2B6ED/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Wolf_0-1679087390501.png" alt="Wolf_0-1679087390501.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2023 21:11:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/error-populating-the-fields-for-the-selected-layer/m-p/1269139#M9554</guid>
      <dc:creator>Wolf</dc:creator>
      <dc:date>2023-03-17T21:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: Error populating the fields for the selected layer in a dockpane</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/error-populating-the-fields-for-the-selected-layer/m-p/1269189#M9556</link>
      <description>&lt;P&gt;Hi Wolf,&lt;/P&gt;&lt;P&gt;Thanks for your help! After rereading your post, I understood what you meant. I am not able to open the example you attached. I recieved the following error message: "error : The project file cannot be opened. Unable to locate the .NET SDK. Check that it is installed and that the version specified in global.json (if any) matches the installed version." I have ArcGIS Pro SDK 2.8 installed. It looks like the SDK version does not match, most likely due to 3.x migration and SDK updates.&amp;nbsp;Instead of creating a new collection, can you show me a short snippet of what I should be doing to see the layer's fields?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thi&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2023 16:40:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/error-populating-the-fields-for-the-selected-layer/m-p/1269189#M9556</guid>
      <dc:creator>tzz_12</dc:creator>
      <dc:date>2023-03-20T16:40:34Z</dc:date>
    </item>
    <item>
      <title>Re: Error populating the fields for the selected layer in a dockpane</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/error-populating-the-fields-for-the-selected-layer/m-p/1269508#M9560</link>
      <description>&lt;P&gt;Hi Thi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;I attached a 2.9 sample that should build and run in 2.8.&amp;nbsp; Remember that when you add data to the UI as a result of an Event (I.E. layer changed events) then very likely your code will run on a non-UI thread.&amp;nbsp; Also anything within QueuedTask.Run is running on a non-UI thread.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2023 17:33:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/error-populating-the-fields-for-the-selected-layer/m-p/1269508#M9560</guid>
      <dc:creator>Wolf</dc:creator>
      <dc:date>2023-03-20T17:33:47Z</dc:date>
    </item>
    <item>
      <title>Re: Error populating the fields for the selected layer in a dockpane</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/error-populating-the-fields-for-the-selected-layer/m-p/1269706#M9562</link>
      <description>&lt;P&gt;For anyone having the same issue, I made a few changes to the DockpaneWithLayerFieldSelection, but both ways work:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt; internal class DockpaneWithLayerFieldSelectionViewModel : DockPane
  {
    private const string _dockPaneID = "DockpaneWithLayerFieldSelection_DockpaneWithLayerFieldSelection";

    private ObservableCollection&amp;lt;FeatureLayer&amp;gt; _layers = new ObservableCollection&amp;lt;FeatureLayer&amp;gt;();
    private ObservableCollection&amp;lt;string&amp;gt; _fieldNames = new ObservableCollection&amp;lt;string&amp;gt;();
    private object _lock = new object();

    protected DockpaneWithLayerFieldSelectionViewModel() 
    {
      BindingOperations.EnableCollectionSynchronization(_layers, _lock);
      BindingOperations.EnableCollectionSynchronization(_fieldNames, _lock);
      LayersAddedEvent.Subscribe(OnLayersAdded);
      LayersRemovedEvent.Subscribe(OnLayersRemoved);
      ActiveMapViewChangedEvent.Subscribe(OnActiveMapViewChanged);
      // just in case the map is already up
      if (MapView.Active != null)
      {
        ActiveMapViewChangedEventArgs args = new ActiveMapViewChangedEventArgs(MapView.Active, null);
        OnActiveMapViewChanged(args);
      }
    }

    #region Bindings

    public ObservableCollection&amp;lt;FeatureLayer&amp;gt; Layers
    {
      get { return _layers; }
    }

    private FeatureLayer _selectedLayer;
    public FeatureLayer SelectedLayer
    {
      get { return _selectedLayer; }
      set
      {
        SetProperty(ref _selectedLayer, value, () =&amp;gt; SelectedLayer);
        FieldNames.Clear();
        if (SelectedLayer == null) return;
        QueuedTask.Run(() =&amp;gt;
        {
          foreach (FieldDescription fd in SelectedLayer?.GetFieldDescriptions())
          {
            string shapeField = SelectedLayer.GetFeatureClass().GetDefinition().GetShapeField();
            if (fd.Name == shapeField) continue;
            FieldNames.Add(fd.Name);
          }
        });
      }
    }

    public ObservableCollection&amp;lt;string&amp;gt; FieldNames
    {
      get { return _fieldNames; }
    }

    private string _selectedFieldName;
    public string SelectedFieldName
    {
      get { return _selectedFieldName; }
      set
      {
        SetProperty(ref _selectedFieldName, value, () =&amp;gt; SelectedFieldName);
        Selection = string.Empty;
        if (SelectedFieldName == null) return;
        Selection = $@"{SelectedLayer.Name}.{SelectedFieldName}";
      }
    }

    private string _Selection;
    public string Selection
    {
      get =&amp;gt; _Selection;
      set =&amp;gt; SetProperty(ref _Selection, value);
    }

    /// &amp;lt;summary&amp;gt;
    /// Text shown near the top of the DockPane.
    /// &amp;lt;/summary&amp;gt;
    private string _heading = "Show Layer and Field Selection";
    public string Heading
    {
      get =&amp;gt; _heading;
      set =&amp;gt; SetProperty(ref _heading, value);
    }

    #endregion

    #region Event Handlers

    /// &amp;lt;summary&amp;gt;
    /// The active map view changed, first clear layers and fields, then
    /// refresh with new layers if incomingView is not null
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name="args"&amp;gt;&amp;lt;/param&amp;gt;
    private void OnActiveMapViewChanged(ActiveMapViewChangedEventArgs args)
    {
      FieldNames.Clear();
      Layers.Clear();
      if (args.IncomingView != null)
      {
        var layers = args.IncomingView.Map.GetLayersAsFlattenedList().OfType&amp;lt;FeatureLayer&amp;gt;();
        Layers.AddRange(layers);
      }
    }

    private void OnLayersRemoved(LayerEventsArgs args)
    {
      foreach (var layer in args.Layers.OfType&amp;lt;FeatureLayer&amp;gt;())
      {
        if (Layers.Contains(layer))
          Layers.Remove((FeatureLayer)layer);
      }
    }

    private void OnLayersAdded(LayerEventsArgs args)
    {
      foreach (var layer in args.Layers.OfType&amp;lt;FeatureLayer&amp;gt;())
      {
        Layers.Add((FeatureLayer)layer);
      }
    }

    #endregion

    /// &amp;lt;summary&amp;gt;
    /// Show the DockPane.
    /// &amp;lt;/summary&amp;gt;
    internal static void Show()
    {
      DockPane pane = FrameworkApplication.DockPaneManager.Find(_dockPaneID);
      if (pane == null) return;
      pane.Activate();
    }
  }

  /// &amp;lt;summary&amp;gt;
  /// Button implementation to show the DockPane.
  /// &amp;lt;/summary&amp;gt;
	internal class DockpaneWithLayerFieldSelection_ShowButton : Button
  {
    protected override void OnClick()
    {
      DockpaneWithLayerFieldSelectionViewModel.Show();
    }
  }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 00:39:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/error-populating-the-fields-for-the-selected-layer/m-p/1269706#M9562</guid>
      <dc:creator>tzz_12</dc:creator>
      <dc:date>2023-03-21T00:39:30Z</dc:date>
    </item>
  </channel>
</rss>

