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

Dynamically apply CellStyleSelector from code behind

1 Answer 90 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Prasant
Top achievements
Rank 1
Prasant asked on 13 Jul 2016, 08:56 AM

I have a cellstyleselector

public class MyStyleSelector : StyleSelector
    {
        public override Style SelectStyle(object item, DependencyObject container)
        {
            return MyStyle;
        }
        public Style MyStyle { get; set; }
    }

I have a grid called DashboardGrid where I add columns in the code behind as they are dependent on columns which the user wants to see. I do this in the following code.

 

foreach (var deliverable in viewModel.Profile.Deliverables)
{

                GridViewDataColumn myColumn = new GridViewDataColumn();
                myColumn.DataMemberBinding = new Binding(deliverable.Path + ".My")
                {
                    Converter = dateToWeekNumberConverter //Using a converter
                };
                myColumn.Header = "Col1";
                myColumn.UniqueName = deliverable.Path + ".My";
                Style style = new Style(typeof(GridViewCell));
                Setter setter = new Setter(GridViewCell.BackgroundProperty, new Binding(deliverable.Path + ".MyBackgroundBrush"));
                style.Setters.Add(setter);
                DeliverableStyleSelector deliverableStyle = new DeliverableStyleSelector();
                deliverableStyle.DeliverableStyle = style;
                myColumn.CellStyleSelector = deliverableStyle;
                this.DashBoardGrid.Columns.Add(myColumn);

 

                GridViewDataColumn myColumn2 = new GridViewDataColumn();
                myColumn2.DataMemberBinding = new Binding(deliverable.Path + ".My2")
                {
                    Converter = dateToWeekNumberConverter //Using a converter
                };
                myColumn2.Header = "Col1";
                myColumn2.UniqueName = deliverable.Path + ".My2";
                Style style = new Style(typeof(GridViewCell));
                Setter setter = new Setter(GridViewCell.BackgroundProperty, new Binding(deliverable.Path + ".MyBackgroundBrush2"));
                style.Setters.Add(setter);
                DeliverableStyleSelector deliverableStyle = new DeliverableStyleSelector();
                deliverableStyle.DeliverableStyle = style;
                myColumn2.CellStyleSelector = deliverableStyle;
                this.DashBoardGrid.Columns.Add(myColumn2);

}

When 'My' > 'My2' then the MyBackgroundBrush is changed to Brushes.GreenYellow. The code works fine until I scroll through the grid. Then the cell styles are gone. PFA the screenshots. Please help me.

 

Note:  I can't add the styles in the xaml as I dont know how many deliverables are going to come. It can range from 1 deliverable to 100 and their setter is dependent on the MyBackgroundBrush and binding, deliverable.Path whose values again will be different as the deliverables.

 

1 Answer, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 14 Jul 2016, 11:33 AM
Hi Prasant,

Generally, the CellStyleSelector is used for conditionally applying different styles for the cell. In your case, according to the MyCellStyleSelector definition, the same style is applied to each cell. In such case, you can directly create a Style that targets GridViewCell and it to the CellStyle property of the desired column. Please check the CellStyleSelector article from our documentation and consider if you need to apply it or you would rather use CellStyle.

Furthermore, I cannot see the definition of the DeliverableStyleSelector. Would it be possible to provide a sample project so I can get a better idea of your overall setup?

Regards,
Stefan Nenchev
Telerik by Progress
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
Prasant
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Share this question
or