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

CellTemplateSelector not refresh Cell Template

6 Answers 533 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sergey
Top achievements
Rank 1
Sergey asked on 30 Jan 2013, 02:04 PM
Hello.

I have a gridView with column. I need to change column template in run-time. I create CellTemplateSelector, but my cell template is not updating. Grid cell template is changing when I scroll the gridView. What is wrong?

        <convertors:TrendConverter x:Key="TrendConverter" />
        <convertors:ConditionalDataTemplateSelector x:Key="selector" ConditionConverter="{StaticResource TrendConverter}">
            <convertors:ConditionalDataTemplateSelector.Rules>
                <convertors:ConditionalDataTemplateRule DataTemplate="{StaticResource d1}">
                    <convertors:ConditionalDataTemplateRule.Value>
                        <sys:Int32>0</sys:Int32>
                    </convertors:ConditionalDataTemplateRule.Value>
                </convertors:ConditionalDataTemplateRule>
                <convertors:ConditionalDataTemplateRule DataTemplate="{StaticResource d2}">
                    <convertors:ConditionalDataTemplateRule.Value>
                        <sys:Int32>1</sys:Int32>
                    </convertors:ConditionalDataTemplateRule.Value>
                </convertors:ConditionalDataTemplateRule>
                <convertors:ConditionalDataTemplateRule DataTemplate="{StaticResource d3}">
                    <convertors:ConditionalDataTemplateRule.Value>
                        <sys:Int32>-1</sys:Int32>
                    </convertors:ConditionalDataTemplateRule.Value>
                </convertors:ConditionalDataTemplateRule>
            </convertors:ConditionalDataTemplateSelector.Rules>
        </convertors:ConditionalDataTemplateSelector>
 
<telerik:GridViewDataColumn Header="Посл." UniqueName="TrendValue" DataMemberBinding="{Binding Path=Trend}" Style="{DynamicResource GridViewDataColumnIntegerStyle}" CellTemplateSelector="{StaticResource selector}">

Thanks

6 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 01 Feb 2013, 02:34 PM
Hello,

Generally every time when the property bound to the column (with the CellTemplateSelector) changes, the CellTemplateSelector should be re-evaluated. 

Then you say that the cell template is changing when I scroll the gridView. This would happen if you work with the visual elements (i.e. GridViewCell) and its properties. Is that the case with you?

For both the problems, it would be helpful if you could send us a simple demo showing the full implementation you have related to the CellTemplateSelector. That way we can debug it locally and check what happens.

Greetings,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
trinh
Top achievements
Rank 1
answered on 18 Feb 2013, 03:10 AM
Hi Didie,

I also have this bug. Yes, I work with the visual elements GridViewCell and its properties. I use the ContentPresenter and DataTemplate to create template for gridviewcell.

ContentPresenter:
public class CellContentPresenter : ContentPresenter
    {
        public CellContentPresenter()
        {
            this.Loaded += (s, e) =>
                {
                    // Create a binding with defined cell datatemplate 
                    var parentCell = this.ParentOfType<GridViewCell>();

                    if (parentCell != null)
                    {
                        var parentBinding = parentCell.DataColumn.DataMemberBinding;
                        
                        var binding = new Binding();
                        binding.Path = parentBinding.Path;
                        binding.Mode = BindingMode.TwoWay;
                        binding.Converter = parentBinding.Converter;
                        binding.ConverterParameter = parentCell.DataColumn.DataMemberBinding.ConverterParameter;
                        
                        // make a binding
                        this.SetBinding(ContentPresenter.ContentProperty, binding);
                    }
                };
        }
    }

Thank to tell me how to fix this bug.


0
Dimitrina
Telerik team
answered on 18 Feb 2013, 01:41 PM
Hello,

Why do you need to set the Binding that way? You could just return a proper template based on the property as shown in this documentation article.

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
trinh
Top achievements
Rank 1
answered on 19 Feb 2013, 09:50 AM
I need set binding because my CellTemplateSelector shall be used for more column. The data shall be bind from value of corresponding cell
0
Dimitrina
Telerik team
answered on 19 Feb 2013, 09:54 AM
Hello,

Probably the question should be why don't you implement the CellTemplateSelector as shown on the help article? There is Binding set when the DataTemplate is defined.
 

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
trinh
Top achievements
Rank 1
answered on 19 Feb 2013, 10:08 AM
The number columns of my grid is dynamic. So, the CellTemplateSelector and DataTemplate shall be built in code.

My DataTemplate:
var cellTemp = new StringBuilder();
            cellTemp.Append("<DataTemplate ");
            cellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
            cellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
            cellTemp.Append("xmlns:d='http://schemas.microsoft.com/expression/blend/2008' ");
            cellTemp.Append("xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' ");
            cellTemp.Append("xmlns:local='clr-namespace:XXX.Library;assembly=Infratructure.Library' ");
            cellTemp.Append("xmlns:tb='clr-namespace:XXX.Library.Controls;assembly=Infratructure.Library' ");
            cellTemp.Append(">");
            cellTemp.Append("<local:CellContentPresenter>");
            cellTemp.Append("<local:CellContentPresenter.ContentTemplate>");
            cellTemp.Append("<DataTemplate>");
            cellTemp.Append("<tb:mycontrol Text='{Binding}' />");
            cellTemp.Append("</DataTemplate>");
            cellTemp.Append("</local:CellContentPresenter.ContentTemplate>");
            cellTemp.Append("</local:CellContentPresenter>");
            cellTemp.Append("</DataTemplate>");
Tags
GridView
Asked by
Sergey
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
trinh
Top achievements
Rank 1
Share this question
or