In WPF ,you will do this by using DataBinding ,First we have a DataModel Class which hold the number,name,colorname respectively..then add the following code
XAML Code:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel >
<TextBlock Text="{Binding Number}" Margin="2"></TextBlock>
<TextBlock Text="{Binding ColorName}" Margin="2"></TextBlock>
<StackPanel Background="{Binding color,Converter={StaticResource ColorConvertor}}" Margin="5"></StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
----------------------------------------------------------------------------------
.CS Code:
public class MyColorConvertor :IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string colorcode = value.ToString();
if(colorcode=="Red")
return new SolidColorBrush(Colors.Red);
else if (colorcode == "Green")
return new SolidColorBrush(Colors.Green);
return new SolidColorBrush(Colors.White);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}