Dynamically apply CellStyleSelector from code behind

2 posts, 0 answers
  1. Prasant
    Prasant avatar
    1 posts
    Member since:
    Jul 2016

    Posted 13 Jul Link to this post

    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.

     

  2. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    281 posts

    Posted 14 Jul Link to this post

    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 apply 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.
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top