RadColorBox BindingSource

2 posts, 0 answers
  1. Mohamed
    Mohamed avatar
    1 posts
    Member since:
    Jan 2015

    Posted 31 May 2015 Link to this post

    how can i use a binding source to bind values to radColorBox  while binding source is list of string of color names
  2. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 01 Jun 2015 Link to this post

    Hi Mohamed,

    Thank you for writing.

    You should create a Binding and use it Format and Parse events to convert from String to Color and vice versa. For example:
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
        
            BindingList<ColorInfo> colorNames = new BindingList<ColorInfo>();
     
            colorNames.Add(new ColorInfo() { ColorName = "Red" });
            colorNames.Add(new ColorInfo() { ColorName = "Green" });
            colorNames.Add(new ColorInfo() { ColorName = "Blue" });
     
            BindingSource source = new BindingSource();
            source.DataSource = colorNames;
            radBindingNavigator1.BindingSource = source;    
            Binding binding = new Binding("Value", source, "ColorName", true);
     
            binding.Format += binding_Format;
            binding.Parse += binding_Parse;
            radColorBox1.DataBindings.Add(binding);
        }
     
        void binding_Format(object sender, ConvertEventArgs e)
        {
            string val = e.Value.ToString();
            switch (val)
            {
                case "Red":
                    e.Value = Color.Red;
                    break;
                case "Blue":
                    e.Value = Color.Blue;
                    break;
                case "Green":
                    e.Value = Color.Green;
                    break;
                default:
                    e.Value = Color.FromArgb(int.Parse(e.Value.ToString()));
                    break;
            }
        }
     
        void binding_Parse(object sender, ConvertEventArgs e)
        {
            e.Value = ((Color)e.Value).ToArgb().ToString();
        }
    }
     
    class ColorInfo
    {
        public string ColorName { get; set; }
    }

    I hope this will be useful.

    Regards,
    Dimitar
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top