2

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>
Danny Roulaux
  • 57
  • 1
  • 7
  • 3
    How about binding your teamComboBox's SelectedValue to a variable, and then programmatically updating that variable, [such as from this answer](https://stackoverflow.com/a/8916325/3791245)? Otherwise, you should really be pulling from the original list of values given to the ComboBox, instead of creating a new object from the `player`. – Sean Skelly Jul 21 '20 at 20:26

1 Answers1

1

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>
Faywang - MSFT
  • 5,798
  • 1
  • 5
  • 8
  • Thx, that did the job. What does “Content” in the SelectedValuePath exactly do? Cant find anything about it in the MS docs. – Danny Roulaux Jul 22 '20 at 09:05
  • 1
    SelectedValuePath is equivalent to a path, which points to the property of the current SelectedItem. In general, the SelectedItem is a ComboBoxItem, but currently what you need is the data you passed to the ComboBoxItem, you can get the data via ComboBoxItem.Content property. So set the SelectedValuePath to Content. – Faywang - MSFT Jul 22 '20 at 09:25