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?

Upvotes: 0

Views: 52

Answers (1)

joelwsaury
joelwsaury

Reputation: 86

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.

Upvotes: 1

Related Questions