简体   繁体   中英

C# change the selected index of a ComboBox item using LINQ

Im not sure if the title represent the problem I have. The explanation here below should be clearer.

I cannot get my code to select the correct value in the ComboBox with the data from my List. I checked for similar problems here in SO but after reading several I cannot find one that matches my problem.

I fill the list with the follwing code:

if (roleComboBox.SelectionBoxItem.Equals("Player"))
            {
                memberID++;

                Player player = new Player(memberID, team, firstName, lastName, age, salary, yearsActive, position, minutesPerGame);

                players.Add(player);              

                PrintList();
            }

Now I want to retrieve the data from the List and put it back into the TextBox/ComboBox it came from so I can change the data and put it back into the List (kinda a modify option). I have a different ComboBox with where I select an ID which matches a memberID which in its turn retrieves the data from the List. I use the following code for that:

private void ModifyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int modifyID = int.Parse(modifyComboBox.SelectedItem.ToString());
        var player = players.Where(p => p.memberID == modifyID);

        teamComboBox.SelectedValue = player.Select(p => p.team).FirstOrDefault(); // this isnt working as intended
        firstNameTextBox.Text = player.Select(p => p.firstName).FirstOrDefault(); 
        lastNameTextBox.Text = player.Select(p => p.lastName).FirstOrDefault();
        ageTextBox.Text = player.Select(p => p.age.ToString()).FirstOrDefault();
        salaryTextBox.Text = player.Select(p => p.salary.ToString()).FirstOrDefault();
        yearsActiveTextBox.Text = player.Select(p => p.yearsActive.ToString()).FirstOrDefault();
        minutesPerGameTextBox.Text = player.Select(p => p.minutesPerGame.ToString()).FirstOrDefault();
    }

While the TextBoxes get filled with the correct data the ComboBox just gets blank.

This is de XAML code for the ComboBox if needed:

<ComboBox x:Name="teamComboBox" HorizontalAlignment="Left" Margin="200,38,0,0" VerticalAlignment="Top" Width="220">
        <ComboBoxItem IsSelected="True">-Choose a team-</ComboBoxItem>
        <ComboBoxItem>Minnesota Timberwolves</ComboBoxItem>
        <ComboBoxItem>Charlotte Hornets</ComboBoxItem>
        <ComboBoxItem>LA Lakers</ComboBoxItem>
</ComboBox>

In this case, the type of SelectedValue needs is ComboBoxItem instead of the string type you passed. If you want to set the SelectedValue programmatically, you can set the SelectedValuePath of teamComboBox as " Content " first, it means to set/get the Content of the ComboBoxItem(ie the string you set for the ComboBoxItem). Then you can set your p.team to SelectedValue. For example:

.xaml:

<ComboBox x:Name="teamComboBox" HorizontalAlignment="Left" SelectedValuePath="Content" Margin="200,38,0,0" VerticalAlignment="Top" Width="220">
    <ComboBoxItem IsSelected="True">-Choose a team-</ComboBoxItem>
    <ComboBoxItem>Minnesota Timberwolves</ComboBoxItem>
    <ComboBoxItem>Charlotte Hornets</ComboBoxItem>
    <ComboBoxItem>LA Lakers</ComboBoxItem>
</ComboBox>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM