CellStyleSelector on auto generated columns

7 posts, 0 answers
  1. Gama
    Gama avatar
    18 posts
    Member since:
    Jun 2010

    Posted 02 Feb 2012 Link to this post

    I'm trying to add a CellStyleSelector on autogenerated columns, but no style is being applied.  The grid is below.

    <telerik:RadGridView Name="rgridProgramSearchResults" AutoGenerateColumns="True" AutoGeneratingColumn="rgridProgramSearchResults_AutoGeneratingColumn" CanUserFreezeColumns="False" IsReadOnly="True" Grid.Column="1"
                    ItemsSource="{Binding}" RowIndicatorVisibility="Collapsed" ShowGroupPanel="False" Margin="0,0,0,30" SelectionMode="Multiple" >
        <telerik:RadGridView.Columns>
            <telerik:GridViewSelectColumn UniqueName="Select" />
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>

    and the style
    <me:ImportStyle x:Key="stadiumCapacityStyle">
        <me:ImportStyle.BigStadiumStyle>
            <Style TargetType="telerik:GridViewCell">
                <Setter Property="Background" Value="Red"/>
            </Style>
        </me:ImportStyle.BigStadiumStyle>
        <me:ImportStyle.SmallStadiumStyle>
            <Style TargetType="telerik:GridViewCell">
                <Setter Property="Background" Value="Yellow" />
            </Style>
        </me:ImportStyle.SmallStadiumStyle>
    </me:ImportStyle>

    The auto generate event
    private void rgridProgramSearchResults_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
    {
         e.Column.CellStyleSelector = Application.Current.Resources["stadiumCapacityStyle"] as StyleSelector;
    }


    and the class

    public class ImportStyle : StyleSelector
    {
        public override Style SelectStyle(object item, DependencyObject container)
        {
            return BigStadiumStyle;
     
        }
        public Style BigStadiumStyle { get; set; }
        public Style SmallStadiumStyle { get; set; }
    }
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 03 Feb 2012 Link to this post

    Hi,

     I have tested how the CellStyleSelector is applied to the column inside the AutoGeneratingColumn event and it was applied fine. May you please confirm what is the version that you use? Is the behaviour the same with the latest version?

    Kind regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Gama
    Gama avatar
    18 posts
    Member since:
    Jun 2010

    Posted 06 Feb 2012 Link to this post

    I created a small project below.  As far as I understand, all the columns shoudl be red but no styling is being done.

    MainWindow.xaml
            x:Class="columns.MainWindow"
             xmlns:me="clr-namespace:columns"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.Resources>
     
                <me:ImportStyle x:Key="stadiumCapacityStyle">
                    <me:ImportStyle.BigStadiumStyle>
                        <Style TargetType="telerik:GridViewCell">
                            <Setter Property="Background" Value="Red"/>
                        </Style>
                    </me:ImportStyle.BigStadiumStyle>
                </me:ImportStyle>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <telerik:RadGridView Name="rgridProgramSearchResults" AutoGenerateColumns="True"  CanUserFreezeColumns="False" IsReadOnly="True" Grid.Column="1" AutoGeneratingColumn="rgridProgramSearchResults_AutoGeneratingColumn"
                            ItemsSource="{Binding}" RowIndicatorVisibility="Collapsed" ShowGroupPanel="False" >
            </telerik:RadGridView>
     
        </Grid>
    </Window>
    MainWindow.xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Data;
     
    namespace columns
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DataTable dt = new DataTable();
     
                dt.Columns.Add("Col1");
                dt.Columns.Add("Col2");
                dt.Columns.Add("Col3");
     
                DataRow dr = dt.NewRow();
     
                dr["Col1"] = "row1Col1";
                dr["Col2"] = "row1Col2";
                dr["Col3"] = "row1Col3";
     
                dt.Rows.Add(dr);
                dr = dt.NewRow();
     
                dr["Col1"] = "row2Col1";
                dr["Col2"] = "row2Col2";
                dr["Col3"] = "row2Col3";
     
                dt.Rows.Add(dr);
                dr = dt.NewRow();
     
                dr["Col1"] = "row3Col1";
                dr["Col2"] = "row3Col2";
                dr["Col3"] = "row3Col3";
                dt.Rows.Add(dr);
                rgridProgramSearchResults.ItemsSource = dt;
            }
     
            private void rgridProgramSearchResults_AutoGeneratingColumn(object sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
            {
                e.Column.CellStyleSelector = Application.Current.Resources["stadiumCapacityStyle"] as StyleSelector;
            }
        }
        public class ImportStyle : StyleSelector
        {
            public override Style SelectStyle(object item, DependencyObject container)
            {
                return BigStadiumStyle;
            }
            public Style BigStadiumStyle { get; set; }
        }
    }
  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 07 Feb 2012 Link to this post

    Hi,

     As I can see from your code snippet you have defined the "stadiumCapacityStyle" inside Grid.Resources, then in the code you try to access it from the Application resources, not from the current Grid's resources.

    I have attached a sample project showing how the correct style is applied.

    Regards,
    Didie
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  5. Gama
    Gama avatar
    18 posts
    Member since:
    Jun 2010

    Posted 07 Feb 2012 Link to this post

    Yep, that did it.  Thanks
  6. Eliot
    Eliot avatar
    9 posts
    Member since:
    Dec 2014

    Posted 27 Jan 2015 Link to this post

    Hi,

    This is most of what I was looking for.

    It doesn't however show how you can return a style based on the column header.

    I am trying to return a weekend and weekday style for the entire column.

    The item in the handler is a row.  It doesn't show the columns I am dynamically adding at runtime 



     foreach (DateTime day in EachDay(_startDate, (DateTime)_endDate))
                {

                    var dateColumn = new GridViewExpressionColumn() { Header = day.ToString("dd MMM yyyy"), UniqueName = day.ToString("dd MMM yyyy"), DataFormatString = "{0:##,###}", IsReadOnly = true };

                    dateColumn.CellStyleSelector = Application.Current.Resources["FundingGridColumnStyle"] as StyleSelector;

                    columns.Add(dateColumn);

                    Expression<Func<FUNDING_TRADES, decimal?>> consideration = row => writeConsideration(row, day);
                    GridViewExpressionColumn considerationExpression = this.radGridView.Columns[day.ToString("dd MMM yyyy")] as GridViewExpressionColumn;
                    considerationExpression.Expression = consideration;
                }         

  7. Boris
    Admin
    Boris avatar
    276 posts

    Posted 30 Jan 2015 Link to this post

    Hi Eliot,

    You can easily return a cell style based on the value of its column header. All you need to do is use the container parameter of the SelectStyle() method like so:

    public override Style SelectStyle(object item, DependencyObject container)
          {
              if (container is GridViewCell)
              {
                  var cell = container as GridViewCell;
                  var columnHeader = cell.Column.Header;             
              }
     
              return bigStadium;
          }

    I hope this helps.

    Regards,
    Boris
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top