简体   繁体   English

C# 使用 LINQ 更改 ComboBox 项目的选定索引

[英]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.我无法使用我的列表中的数据获取 select 中 ComboBox 中的正确值的代码。 I checked for similar problems here in SO but after reading several I cannot find one that matches my problem.我在 SO 中检查了类似的问题,但在阅读了几个之后,我找不到与我的问题相匹配的问题。

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).现在我想从 List 中检索数据并将其放回它来自的 TextBox/ComboBox 中,这样我就可以更改数据并将其放回 List(有点修改选项)。 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.我有一个不同的 ComboBox ,其中我 select 的 ID 与 memberID 匹配,而 memberID 又从列表中检索数据。 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.虽然 TextBoxes 填充了正确的数据,但 ComboBox 只是空白。

This is de XAML code for the ComboBox if needed:如果需要,这是 ComboBox 的 de XAML 代码:

<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.在这种情况下,SelectedValue 需要的类型是 ComboBoxItem 而不是你传递的字符串类型。 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).如果你想以编程方式设置SelectedValue,你可以先将teamComboBox的SelectedValuePath设置为“ Content ”,意思是设置/获取ComboBoxItem的Content(即你为ComboBoxItem设置的字符串)。 Then you can set your p.team to SelectedValue.然后您可以将您的 p.team 设置为 SelectedValue。 For example:例如:

.xaml: .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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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