'ComboBox, How to give value

I'm trying to assign a value to My ComboBox with the name "tbYear", this value I want to assign comes from My API. I'm getting the value correctly, I just have to be able to realize this piece of code. I'm using UWP, C#.

My TreeView in file.xaml

<ComboBox x:Name="tbYear">
            <ComboBoxItem Content="Year 1"></ComboBoxItem>
            <ComboBoxItem Content="Year 2"></ComboBoxItem>
</ComboBox>

My file.xaml.cs

private async void GetData
{
        srcResults = await apiclient.GetValues();
        tbYear.SelectedItem = srcResults.Year;
}

But i get no error but don't work

How can I solve it?



Solution 1:[1]

Set SelectedValuePath="Content"

<ComboBox x:Name="tbYear" SelectedValuePath="Content">
            <ComboBoxItem Content="Year 1"></ComboBoxItem>
            <ComboBoxItem Content="Year 2"></ComboBoxItem>
</ComboBox>

And then you would probably want to set SelectedValue, such as:

private async void GetData
{
        srcResults = await apiclient.GetValues();
        tbYear.SelectedValue = srcResults.Year;
}

I would suggest to use GetDataAsync() as name, make sure to return Task instead of void also.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 joelwsaury