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!!
Â