Select to view content in your preferred language

enable/disable button on dock pane based on selection

101
2
Jump to solution
Thursday
MichaelKohler
Frequent Contributor

trying to enable/disable button on a dock pane based on feature layer selection. if there is a selection on the feature layer, i want to merge the features and put in a different feature layer.

I've tried binding and searched for some examples through, i have to be honest, Chat GPT. This binding thing is new for me. 😞  The code compiles and builds but the enabled/disabled state doesn't change and there are no errors and nothing happens.

Currently, I've added a test to see if there are selected features to the button click and that works fine. But I would like to one, learn how to get this to work, and two, make the UI better for the user.

.xaml

<Button x:Name="BtnCreateBndy" Content="Run Tool" Command="{Binding RunCommand}" IsEnabled="{Binding IsButtonEnabled}"/>

 

The code in my view model

 protected PAMDPViewModel() 
 {
     // Subscribe to selection changes
     MapSelectionChangedEvent.Subscribe(OnSelectionChanged);
 }

 private bool _isButtonEnabled;
 public bool IsButtonEnabled
 {
     get => _isButtonEnabled;
     set => SetProperty(ref _isButtonEnabled, value);  // Notifies UI
 }

 public ICommand RunCommand => new RelayCommand(() =>
 {
     ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Button clicked.");
     // Your button click logic
 }, () => IsButtonEnabled);


 private void OnSelectionChanged(MapSelectionChangedEventArgs args)
 {
     QueuedTask.Run(() =>
     {
         bool enable = false;

         var selection = MapView.Active?.Map?.GetSelection();
         if (selection != null)
         {
             foreach (var kvp in selection.ToDictionary())
             {
                 var layer = kvp.Key as FeatureLayer;
                 var oids = kvp.Value;

                 if (layer != null && oids.Count > 0 && layer.Name == "Parcel Owner")
                 {
                     enable = true;
                     break;
                 }
             }
         }

         // Now safely update UI-bound property on UI thread
         Application.Current.Dispatcher.BeginInvoke(new Action(() =>
         {
             IsButtonEnabled = enable;

             ((RelayCommand)RunCommand).RaiseCanExecuteChanged();

         }));
     });
 }

 

 

Thank you!!

 

0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

Your code works fine. Check layer name in TOC. Another one thing is your layer selectable?

View solution in original post

0 Kudos
2 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Your code works fine. Check layer name in TOC. Another one thing is your layer selectable?

0 Kudos
MichaelKohler
Frequent Contributor

to answer the questions, yes the name is correct and it is the only selectable layer in the map.

I'm not entirely sure why???? but you were right. It works.

I commented everything out and implemented the check for selection in the button click. Then stepped out for an errand. Saw your post and switched back to this solution and it worked!

 

0 Kudos