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.
Unfortunately, activation email could not send to your email. Please try again.

How to Add ColorPicker to DataTemplate

Thread ID:

Created:

Updated:

Platform:

Replies:

115109 Dec 24,2013 05:13 AM Feb 10,2014 07:44 AM WPF 3
loading
Tags: GridControl
Sharron Denice
Asked On December 24, 2013 05:13 AM

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...

[code]
                    else if (type == typeof(Color))
                    {
                        DataTemplate colorTemplate = new DataTemplate();
                        colorTemplate.DataType = typeof(string);

                        // WHAT DO I DO HERE TO ADD A COLORPICKER TO THE TEMPLATE

                        GridDataColumnStyle style = new GridDataColumnStyle();
                        style.CellItemTemplate = colorTemplate;
                        col.ColumnStyle = style;
                    }
[/code]

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

Sharron Denice
Replied On December 24, 2013 05:33 AM

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

[code]
                        // 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;
[/code]

Sharron Denice
Replied On December 24, 2013 07:47 AM

ANSWER: 

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...

[code]
                        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;
[/code]

Saravanan M [Syncfusion]
Replied On February 10, 2014 07:44 AM

Hi Sharron, 

We are sorry about not clear in previous update, 

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

Regards,

Saravanan.M


CONFIRMATION

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.

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.

;