RadListDataItem backcolor

14 posts, 1 answers
  1. Giovanni Riccò
    Giovanni Riccò avatar
    6 posts
    Member since:
    Jul 2012

    Posted 18 Nov 2010 Link to this post

    Hi,

    I could easily change the RadListDataItem ForeColor by setting its ForeColor property, but I can't find out the way to change its backcolor, instead. What should I do?

    Thanks, 
    Giovanni
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 18 Nov 2010 Link to this post

    Hi,

    Well, you can change the BackColor property but I'd advise against doing it as you will then loose the style that is associated with the theme for selecting items.
    If you want to, then you can change the backcolor in this way:
    For Each item As RadListDataItem In Me.RadListControl1.Items
        item.VisualItem.SetValue(LightVisualElement.BackColorProperty, System.Drawing.Color.Red)
        item.VisualItem.SetValue(LightVisualElement.GradientStyleProperty, GradientStyles.Solid)
    Next

    but in my view, you'd be better off creating a theme for your items in Visual Style Builder. You can find out more about VSB at this link

    hope that helps
    richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 21 Nov 2010 Link to this post

    Hello

    Did this help? If so, please mark as answer so others can find the solution too
    regards,
    Richard
  5. Giovanni Riccò
    Giovanni Riccò avatar
    6 posts
    Member since:
    Jul 2012

    Posted 22 Nov 2010 Link to this post

    Hi Richard,

    this didn't help, because VisualElement property is null, so I get an exception while attempting to set its background color.
    I need an alternative.

    All the best,
    Giovanni
  6. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 22 Nov 2010 Link to this post

    Hello,

    I'd advise using the Visual Stye Builder as above to build a new theme that has a background colour that you want to use. If you would like to use the code posted above however, then if you post me a sample of your code then I will happily take a look at it for you.

    Thanks
    Richard
  7. Giovanni Riccò
    Giovanni Riccò avatar
    6 posts
    Member since:
    Jul 2012

    Posted 22 Nov 2010 Link to this post

    Hi Richard,

    I thought that there would be a simple way of doing this, just as for the ForeColor:

    RadListControl radList = new RadListControl();
    foreach(Object myObject in myList)
    {
    RadListDataItem item = new RadListDataItem();
    radList.Items.Add(item);
    //I can't access item.BackColor, only Font or ForeColor
    item.ForeColor = Color.Red;
    item.DataBoundItem = myObject;
    }

    I can't follow the same way because BackColor property is not exposed, and item.VisualItem is null. Is Visual Style Builder the only way?

    All the best,
    Giovanni
  8. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 22 Nov 2010 Link to this post

    Hi,

    Straight after adding the items in the loop that you have in your code, please can you try adding this loop to set the colour. The VisualItem should not be null in this case.

    foreach (RadListDataItem item in this.radList.Items) {
        item.VisualItem.SetValue(LightVisualElement.BackColorProperty, System.Drawing.Color.Red);
        item.VisualItem.SetValue(LightVisualElement.GradientStyleProperty, GradientStyles.Solid);
    }

    Richard

    EDIT// I'd still say that going down the VSB route would be best though to avoid incompatibilities.
  9. Giovanni Riccò
    Giovanni Riccò avatar
    6 posts
    Member since:
    Jul 2012

    Posted 22 Nov 2010 Link to this post

    Hi Richard,

    the VisualItem property is still null, even after the addItem loop. I understand that the only way is VSB, isn't it?
    Could I suggest you to expose BackColor property just as ForeColor?

    Best regards,
    Giovanni
  10. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 22 Nov 2010 Link to this post

    Hello,

    I don't work for Telerik. I'm just another user of the forums, but if you have a feature suggestion you can request one via a support ticket.

    Can you confirm if you are using a RadDropDownList or a RadListControl
    Your code indicates you are using a RadListControl, but I have the feeling you are using a RadDropDownList. Please can you confirm.
    Thanks
    Richard
  11. Giovanni Riccò
    Giovanni Riccò avatar
    6 posts
    Member since:
    Jul 2012

    Posted 22 Nov 2010 Link to this post

    Hi,

    I'm using a RadListControl as you can see.

    Giovanni
  12. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 22 Nov 2010 Link to this post

    Hello,

    Please subscribe to the VisualItemFormatting event and add the code below

    private void radListControl1_VisualItemFormatting(object sender, VisualItemFormattingEventArgs args)
    {
        args.VisualItem.SetValue(LightVisualElement.BackColorProperty, System.Drawing.Color.Red);
        args.VisualItem.SetValue(LightVisualElement.GradientStyleProperty, GradientStyles.Solid);
    }

    Let me know if this works
    thanks
    Richard
  13. Giovanni Riccò
    Giovanni Riccò avatar
    6 posts
    Member since:
    Jul 2012

    Posted 22 Nov 2010 Link to this post

    Hi Richard,

    this works!

    Thanks a lot,
    Giovanni
  14. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 22 Nov 2010 Link to this post

    Excellent, Glad to have been able to help
    Richard
  15. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 23 Nov 2010 Link to this post

    Hi Giovanni, I am glad to hear that this solved the issue. In case you need further assistance, please do not hesitate to contact us.

    Richard, thank you for sharing your solution with the community.

    Sincerely yours, Peter
    the Telerik team
    Get started with RadControls for WinForms with numerous videos and detailed documentation.
Back to Top
UI for WinForms is Visual Studio 2017 Ready