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.