This is a migrated thread and some comments may be shown as answers.

Set Backgroud color for each item

2 Answers 116 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Gianfranco
Top achievements
Rank 1
Gianfranco asked on 29 Oct 2019, 09:36 AM

Hi everyone

I need to give each item in a RadCombobox a very specific color..

My XAML is:

<telerik:RadComboBox x:Name="cmbToolTypeForLoad" ItemsSource="{Binding ToolTypeForCmb}" DisplayMemberPath="Description"  Margin="46,29,0,0"                          HorizontalAlignment="Left" VerticalAlignment="Top" Width="421"  Height="48"  >
                    <ComboBoxItem>
                        <Style TargetType="ComboBoxItem">
                            <Setter Property="Background" Value="{Binding Color}"/>
                        </Style>
                    </ComboBoxItem>
                </telerik:RadComboBox>

 

to populate my combobox I had to convert the integer color stored in SQL:

 

 public partial class ToolTypeForCmb
    {
        public int ToolTypeId { get; set; }
        public string Description { get; set; }
        public Brush Color { get; set; }
    }

 

       List<ToolType> toolTypes = toolTypeService.GetAll(MachineryCode);
       List<ToolTypeForCmb> toolTypeForCmb = new List<ToolTypeForCmb>();
       foreach (ToolType toolType in toolTypes)
       {
                ToolTypeForCmb ttfcmb = new ToolTypeForCmb();
                ttfcmb.Color = UtilityService.ConvertIntToColorBrush(toolType.Color);
                ttfcmb.Description = toolType.Description;
                ttfcmb.ToolTypeId = toolType.ToolTypeId;
                toolTypeForCmb.Add(ttfcmb);
        }

       cmbToolTypeForLoad.ItemsSource = toolTypeForCmb;

 

Unfortunately, it generates an error when I assign the item.

"The collection of items must be empty before you can use ItemsSource".

 

how can i solve?

thanks

 

2 Answers, 1 is accepted

Sort by
0
Gianfranco
Top achievements
Rank 1
answered on 29 Oct 2019, 10:02 AM

I solved with:
in Resource
<Style x: Key = "ItemStyle" TargetType = "telerik: RadComboBoxItem">
    <Setter Property = "Background" Value = "{Binding Color}" />
</ Style>


then

<telerik: RadComboBox x: Name = "cmbToolTypeForLoad" ItemsSource = "{Binding ToolTypeForCmb}" ItemContainerStyle =

      "{StaticResource ItemStyle}" DisplayMemberPath = "Description" Margin = "46.29.0.0" HorizontalAlignment = "Left"

      VerticalAlignment = "Top" Width = "421" Height = "48">
</ Telerik: RadComboBox>

Sorry

 

0
Martin Ivanov
Telerik team
answered on 31 Oct 2019, 07:37 AM

Hello Gianfranco,

This is the way to go. Thank you for sharing your solution.

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
ComboBox
Asked by
Gianfranco
Top achievements
Rank 1
Answers by
Gianfranco
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or