マリオバインダーの答えに加えて、ここにそのようなコンバーターの例があります:
public class ListToColoredComboboxItemsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is IEnumerable<Measurement> measurements)
{
var comboBoxItems = new List<ComboBoxItem>(measurements.Count());
foreach (var measurement in measurements)
{
var item = new ComboBoxItem();
item.Content = measurement;
if (!string.IsNullOrWhiteSpace(measurement.ErrorMessage))
item.Foreground = Brushes.Red;
comboBoxItems.Add(item);
}
return comboBoxItems;
}
return null;
}
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
また、選択したアイテムを値に戻すこともできます。
public class ComboBoxItemToItemConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
{
if (value is ComboBoxItem comboBoxItem)
{
return comboBoxItem.Content;
}
return null;
}
}