2,854 questions
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;
}
}
}