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

defining and using styles in code behind

1 Answer 878 Views
GridView
This is a migrated thread and some comments may be shown as answers.
al
Top achievements
Rank 1
al asked on 05 Feb 2017, 03:11 PM

i have created a derived class from telerik:RadGridView. i need to styling the cells based on the data. so that i have created a styling class and use it as cellstyleselector for columns. i need to sett column cell style for each column and i used the below code but it does not work. do you have any idea of why it does not works?

public class WPFGridConv:RadGridView
{
public WPFGridConv()
    {
        this.ShowGroupPanel = false;
        this.Loaded += WPFGridConv_Loaded;
        this.AutoGeneratingColumn += WPFGridConv_AutoGeneratingColumn;
        this.ValidationType = GridViewValidationType.None;
        this.ValidatesOnDataErrors= GridViewValidationMode.InViewMode;
 
    }
 
    private void WPFGridConv_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
    {
        CreateHeader();
    }
 
 
    public void WPFGridConv_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        CreateHeader();
 
    }
 
    private void CreateHeader()
    {
 
        foreach (Telerik.Windows.Controls.GridViewColumn column in this.Columns)
        {
 
                column.CellStyleSelector= new GridStyleSelector();
 
        }
    }
}
 
public class GridStyleSelector : StyleSelector
{
 
    public override Style SelectStyle(object item, DependencyObject container)
    {
        if (container is GridViewCell)
        {
            GridViewCell cell = container as GridViewCell;
            string columnId = cell.Column.Tag.ToString();
            WPFGridConv grid = cell.ParentOfType<WPFGridConv>();
            List<InputClass.GridColumnInfoClass> columnsInfoList = grid.workingGridClass.ListOfColumnsInfo;
            InputClass.GridColumnInfoClass columnInfo = columnsInfoList.Find(x => x.id == columnId);
            Type columnType = columnInfo.typeOfColumn;
            if ((cell.Value==null||string.IsNullOrEmpty(cell.Value.ToString())) && columnInfo.IsValueNecessary)
            {
                return NeedeValueStyle;
            }
            else
            {
                if (columnType == null || columnType == typeof(double))
                {
                    double dummyDbl;
                    if (!double.TryParse(cell.Value.ToString(), out dummyDbl))
                        return BadInputStyle;
                    else
                        return CorrectValueStyle;
                }
                else if (columnType == typeof(int))
                {
                    int dummyInt;
                    if (!int.TryParse(cell.Value.ToString(), out dummyInt))
                        return BadInputStyle;
                    else
                        return CorrectValueStyle;
                }
                return CorrectValueStyle;
            }
        }
        return null;
    }
 
    public Style OutOfRangeStyle
    {
        get
        {
            Style returnStyle=new Style(typeof(GridViewCell));
            returnStyle.Setters.Add(new Setter(GridViewCell.BorderBrushProperty, new SolidColorBrush(Colors.Purple)));
            return returnStyle;
        }
        set
        {
 
        }
    }
 
    public Style BadInputStyle {
        get
        {
            Style returnStyle = new Style(typeof(GridViewCell));
            returnStyle.Setters.Add(new Setter(GridViewCell.BorderBrushProperty, new SolidColorBrush(Colors.Red)));
            return returnStyle;
        }
        set
        {
 
        }
    }
 
    public Style NeedeValueStyle
    {
        get
        {
            Style returnStyle = new Style(typeof(GridViewCell));
            returnStyle.Setters.Add(new Setter(GridViewCell.BorderBrushProperty, new SolidColorBrush(Colors.IndianRed)));
            return returnStyle;
        }
        set
        {
 
        }
    }
    public Style CorrectValueStyle
    {
        get
        {
            Style returnStyle = new Style(typeof(GridViewCell));
            returnStyle.Setters.Add(new Setter(GridViewCell.BorderBrushProperty, new SolidColorBrush(Colors.Black)));
            return returnStyle;
        }
        set
        {
 
        }
    }
}

i have created a derived class from telerik:RadGridView. i need to styling the cells based on the data. so that i have created a styling class and use it as cellstyleselector for columns. i need to sett column cell style for each column and i used the below code but it does not work. do you have any idea of why it does not works?

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 08 Feb 2017, 09:49 AM
Hello,

The styles you defined are correctly defined and are indeed applied, however, because of RadGridView's gridlines, the BorderThickness and BorderBrush properties of the cell are not respected. You can test this by setting the Background or Foreground properties instead to see if the styles are applied.

Thus, you will need to edit the control template of the element in order to achieve the desired result. Here's a forum thread discussing a similar scenario.

I hope you find this helpful.

Regards,
Dilyan Traykov
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
al
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or