We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to Add ColorPicker to DataTemplate

Thread ID:





115109 Dec 24,2013 10:13 AM UTC Feb 10,2014 12:44 PM UTC WPF 3
Tags: GridControl
Sharron Denice
Asked On December 24, 2013 10:13 AM UTC

I'm creating a control in my application that autogenerates columns for my datagrid regardless of the object passed to ItemsSource.

However, I am failing to understand how the DataTemplate class operates.

Below is my code...

                    else if (type == typeof(Color))
                        DataTemplate colorTemplate = new DataTemplate();
                        colorTemplate.DataType = typeof(string);


                        GridDataColumnStyle style = new GridDataColumnStyle();
                        style.CellItemTemplate = colorTemplate;
                        col.ColumnStyle = style;

I can accomplish this in WPF but not in C#
            <DataTemplate x:Key="ColorTemplate">
                <syncfusion:ColorPicker Color="{Binding CellBoundValue, Mode=TwoWay}" />
        <syncfusion:GridDataControl x:Name="GridMain"
        VisualStyle="{Binding GridVisualStyle}"
        ItemsSource="{Binding CurrentRecords}" Margin="0,0,10,0">
                <syncfusion:GridDataVisibleColumn HeaderText="Color" MappingName="Color" CellItemTemplate="{StaticResource ColorTemplate}" CellEditItemTemplate="{StaticResource ColorTemplate}"/>
Thank you!

Sharron Denice
Replied On December 24, 2013 10:33 AM UTC

By the way, this is the code I'm using and its not working. It just displays the string Color value in the cell.

                        // First: create the data template for the parent control
                        DataTemplate colorTemplate = new DataTemplate(typeof(ColorPicker));

                        // Second: create and add the colorpicker to the data template
                        FrameworkElementFactory picker = new FrameworkElementFactory(typeof(ColorPicker));
                        colorTemplate.VisualTree = picker;

                        // Create binding
                        Binding bind = new Binding();
                        bind.Path = new PropertyPath(col.MappingName);
                        bind.Mode = BindingMode.TwoWay;

                        // Third: set the binding in the text box
                        picker.SetBinding(ColorPicker.BrushProperty, bind);

                        style.CellItemTemplate = colorTemplate;
                        style.CellEditTemplate = colorTemplate;

Sharron Denice
Replied On December 24, 2013 12:47 PM UTC


Well first I have a typo in the above code but in any case FrameworkElementFactory is deprecated so its of no use anyway. The answer is below...

                        ParserContext pc = new ParserContext();
                        pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
                        pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
                        pc.XmlnsDictionary.Add("syncfusion", "http://schemas.syncfusion.com/wpf");

                        string stringTemplate = "<DataTemplate x:Key=\"ColorTemplate\">";
                        stringTemplate += "<syncfusion:ColorPicker Color=\"{Binding CellBoundValue, Mode=TwoWay}\" />";
                        stringTemplate += "</DataTemplate>";

                        StringReader stringReader = new StringReader(stringTemplate);
                        XmlReader xmlReader = XmlReader.Create(stringReader);
                        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(stringTemplate.ToString()));
                        DataTemplate colorTemplate = (DataTemplate)XamlReader.Load(ms, pc);
                        col.CellItemTemplate = colorTemplate;
                        col.CellEditItemTemplate = colorTemplate;

Saravanan M [Syncfusion]
Replied On February 10, 2014 12:44 PM UTC

Hi Sharron, 

We are sorry about not clear in previous update, 

Thanks for your code . Please let us know if you need further assistance.




This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon