以下に示すコードコメントで強調表示されている以下のエラーについてサポートが必要です。以下のメソッドの目的は、comboxアイテムを値で検索し、存在する場合はコンボボックスに設定することです。それ以外の場合は、コンボボックスに追加してから設定します。
private void SetComboBoxValueHelper(ComboBox cb, string valuetoSet)
{
bool isValueNotFound = false;
cb.SelectedValue = valuetoSet;
isValueNotFound = string.IsNullOrEmpty(Convert.ToString(cb.SelectedValue));
if (isValueNotFound)
{
//try to ignore case and find the item in combobox
foreach (ComboBoxItem item in cb.Items) //1.ERROR AFTER ANY ITEM ADDED using my code
{
if (string.Compare(Convert.ToString(item.Content), valuetoSet, true) == 0)
{
cb.SelectedValue = item.Content;
isValueNotFound = false;
}
}
//if still not found add the item to the combobox
if (isValueNotFound)
{
cb.Items.Add(valuetoSet);
cb.SelectedValue = valuetoSet;//2.THIS IS NOT WORKING
}
}
}
私が使用するサンプルコンボボックスは
<ComboBox Grid.Column="5" Grid.Row="4" Margin="10" Name="cbbox1" SelectedValuePath="Content">
<ComboBoxItem Content="No" IsSelected="True" />
<ComboBoxItem Content="Yes" />
</ComboBox>
a)動作していない回線を修正する方法を教えてください。b)コメントに示されている行でエラーが発生します。どうすればそれを防ぐことができますか。