Looping through ComboBoxItems

bbz9 1 Reputation point
2022-06-20T11:27:52.04+00:00

I want to select item in ComboBox with certain value. For this I created this method:

private int getIndex(ComboBox cb, string value)  
{  
            int index = 0;  
            int actual_index = 0;  
            foreach (ComboBoxItem item in cb.Items)  
            {  
                if (item.Tag.ToString() == value)  
                {  
                    actual_index = index;  
                }  
                index++;  
            }  
            return actual_index;  
}  

However, when I tried to run it, I got error

Unable to cast object of type System.String to type System.Windows.Controls.ComboBoxItem

(sorry if it is inaccurate, I translated it with Google Translate)

What am I doing wrong?

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-06-20T13:13:24.547+00:00

    Since I don't know your data source here is an example of a mocked up ComboBox.

    <Grid>  
        <StackPanel Margin="10,10,10,379">  
            <ComboBox x:Name="ComboBox1">  
                <ComboBoxItem Tag="First">  
                    <StackPanel Orientation="Horizontal">  
                        <TextBlock Foreground="Red">Red</TextBlock>  
                    </StackPanel>  
                </ComboBoxItem>  
                <ComboBoxItem Tag="Second">  
                    <StackPanel Orientation="Horizontal">  
                        <TextBlock Foreground="Green">Green</TextBlock>  
                    </StackPanel>  
                </ComboBoxItem>  
                <ComboBoxItem Tag="Third">  
                    <StackPanel Orientation="Horizontal">  
                        <TextBlock Foreground="Blue">Blue</TextBlock>  
                    </StackPanel>  
                </ComboBoxItem>  
            </ComboBox>  
        </StackPanel>  
        <Button  
            Margin="54,140,617,259"  
            Click="Button_Click"  
            Content="Button" />  
    </Grid>  
    

    Behind

    using System;  
    using System.Diagnostics;  
    using System.Windows;  
    using System.Windows.Controls;  
      
    namespace WpfApp1  
    {  
        public partial class MainWindow : Window  
        {  
            public MainWindow()  
            {  
                InitializeComponent();  
            }  
      
            private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                var findValue = "second";  
                var index = getIndex(ComboBox1, findValue);  
                MessageBox.Show(index > -1 ? $"Index is {index} for '{findValue}'" : $"'{findValue}' Not found");  
            }  
            private int getIndex(ComboBox cb, string value)  
            {  
                int index = 0;  
      
                foreach (ComboBoxItem item in cb.Items)  
                {  
                    if (item.Tag is string currenTagValue)  
                    {  
                        if (string.Equals(currenTagValue, value, StringComparison.OrdinalIgnoreCase))  
                        {  
                            return index;  
                        }  
                    }  
                      
                    index++;  
                }  
      
                return -1;  
            }  
        }  
    }  
      
    
    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.