CellTemplateSelector not refresh Cell Template

7 posts, 0 answers
  1. Sergey
    Sergey avatar
    58 posts
    Member since:
    Jan 2013

    Posted 30 Jan 2013 Link to this post

    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
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 01 Feb 2013 Link to this post

    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.

  3. trinh
    trinh avatar
    11 posts
    Member since:
    Jul 2011

    Posted 17 Feb 2013 Link to this post

    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.


  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 18 Feb 2013 Link to this post

    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.

  5. trinh
    trinh avatar
    11 posts
    Member since:
    Jul 2011

    Posted 19 Feb 2013 Link to this post

    I need set binding because my CellTemplateSelector shall be used for more column. The data shall be bind from value of corresponding cell
  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 19 Feb 2013 Link to this post

    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.

  7. trinh
    trinh avatar
    11 posts
    Member since:
    Jul 2011

    Posted 19 Feb 2013 Link to this post

    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>");
Back to Top