Tasos Theodosiou Tasos Theodosiou - 20 days ago 10
C# Question

How to save selected index of a combobox in UWP and display it back when the user reloads the page?

I have a

ComboBox
control in my UWP application and I need to save the selected index of it when the user is selecting an option! Then, I need to save this index in local settings and when the user is coming back on this page, I want the
ComboBox
to have this saved index as the selected index. I need this functionality in my settings page! Can anyone help me?

This is my code:

private void fuelTypeSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var localSettings = ApplicationData.Current.LocalSettings;

try
{
if(localSettings.Values.ContainsKey("selectedIndex"))
{
int index = (int)localSettings.Values["selectedIndex"];
fuelTypeSelector.SelectedIndex = index;

//update the saved index

if(fuelTypeSelector.SelectedIndex!=index)
{
localSettings.Values["selectedIndex"] =
fuelTypeSelector.SelectedIndex;
}
}
else
{
// index does not exist
localSettings.Values.Add("selectedIndex",
fuelTypeSelector.SelectedIndex);
}
}
catch(Exception ex)
{

}
}

Answer

To get the selected item of ComboBox, you can handle its SelectionChanged event, just for example here:

<ComboBox SelectionChanged="ComboBox_SelectionChanged">
    <ComboBoxItem>Item 1</ComboBoxItem>
    <ComboBoxItem>Item 2</ComboBoxItem>
    <ComboBoxItem>Item 3</ComboBoxItem>
    <ComboBoxItem>Item 4</ComboBoxItem>
    <ComboBoxItem>Item 5</ComboBoxItem>
</ComboBox>

code behind:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //you can get the selected item like this:
    var combo = sender as ComboBox;
    var selecteditem = combo.SelectedItem;

    //or, since ComboBox DOESN'T support multiple selection, you can get the item like:
    var selecteditems = e.AddedItems.FirstOrDefault();
}

Or if you just need the index of this item, you can use the first method and change the code like this: var selectedindex = combo.SelectedIndex;. And of course we can also add items to the Collection of ComboBox through data binding.

By saving the selected item, I personal think it's better to save your local settings when your app is at the suspending stage, and read the settings data at the launched stage. To check the lifecycle of UWP app, the official document Launching, resuming, and background tasks will help you. This means you will have to save your page state during the run time of your app, to do this, you can cache your Setting page, for more information about page state, you can refer to my answer in UWP page state manage.

For saving and retrieving settings part, here is the official documentation: Store and retrieve settings and other app data, there are some sample code in this doc.

Finally since you're new to UWP app development, you can refer to How-to articles for UWP apps on Windows 10 to get started. And there are a lot of official UWP samples on GitHub which may also help.