I have ListBox
where the data source is ObservableCollection<string>
.
I would like the items that started with "WARNING" to have the red font.
I would be able to do this using a DataTemplate
, a Converter
and a template to apply to a Label
or TextBlock
.
But since I do not have DataTemplate
it has changed the scenario. Would you like to add Binding and Convert to a Setter
of style?
<ListBox x:Name="lstLog" Height="160" Width="775">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Height" Value="19" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Converting I know it would look something like this:
public class CorLogConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value.ToString().StartsWith("ATENÇÃO"))
return new SolidColorBrush(Colors.Red);
else
return new SolidColorBrush(Colors.Black);
}
}