Where do methods (such as OnSelectionChange) for a combo box that is nested in a dockpane go?

1124
2
06-03-2019 10:14 AM
JohnPhillipsGeo
New Contributor III

There is plenty of documentation on how to add combo boxes to a toolbar/group from the daml and how to add functionality in the corresponding .cs file.  However, I haven't been able to find any documentation on how to put a combobox in the .xaml file for a dockpane and reference methods.

The method I would like to reference is OnSelectionChange:
https://github.com/Esri/arcgis-pro-sdk/wiki/ProGuide-Combo-boxes#determine-when-a-selection-occurs

I have my combobox declared in the .xaml

<DockPanel Grid.Row="2" Grid.Column="1">
<ComboBox Name ="ProfileBox"
Width="Auto" Height="Auto" IsEditable="True" Background="{DynamicResource Esri_BackgroundPressedBrush}" ItemsSource="{Binding Profiles}"
Foreground="{DynamicResource Esri_TextMenuBrush}">

</ComboBox>
</DockPanel>


and I am able to add items to a list and pass them as the source:

foreach (var item in profileRead)
{
string name = Path.GetFileNameWithoutExtension(item);

_profiles.Add(name);
}

private ObservableCollection<string> _profiles = new ObservableCollection<string>();

public ObservableCollection<string> Profiles
{
set
{
SetProperty(ref _profiles, value, () => Profiles);
NotifyPropertyChanged(() => Profiles);
}

get { return _profiles; }

}

I was able to reference the selection from the xaml.cs file:

public partial class Dockpane1View : UserControl
{
public static ComboBox profileBoxRef = new ComboBox();

public Dockpane1View()
{
InitializeComponent();
profileBoxRef = ProfileBox;
}

}

}

However, I cannot figure out how to implement OnSelectionChange. 

Thanks

JP

2 Replies
GKmieliauskas
Esri Regular Contributor

Hi John,

If you do not use MVVM then you could add event to your xaml file like this:

<ComboBox x:Name="cbLayer" Margin="10" Width="150" SelectionChanged="Layer_SelectionChanged">

In xaml.cs you need to add method for SelectionChanged:

private void Layer_SelectionChanged(object sender, SelectionChangedEventArgs e)

In MVVM you do not need OnSelectionChange. You need to create property for SelectedItem or SelectedValue in your model view file and make binding in xaml

<ComboBox ItemsSource="{Binding Layers}" SelectedItem="{Binding SelectedLayer}"/>

 

JohnPhillipsGeo
New Contributor III

Gintautas,

That worked like a charm, thank you!!

JP

0 Kudos