Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > GridView > CellStyleSelector on auto generated columns

Not answered CellStyleSelector on auto generated columns

Feed from this thread
  • Gama avatar

    Posted on Feb 2, 2012 (permalink)

    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; }
    }

    Reply

  • Didie Didie admin's avatar

    Posted on Feb 3, 2012 (permalink)

    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 >>

    Reply

  • Gama avatar

    Posted on Feb 6, 2012 (permalink)

    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; }
        }
    }

    Reply

  • Didie Didie admin's avatar

    Posted on Feb 7, 2012 (permalink)

    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 >>

    Reply

  • Gama avatar

    Posted on Feb 7, 2012 (permalink)

    Yep, that did it.  Thanks

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > GridView > CellStyleSelector on auto generated columns
Related resources for "CellStyleSelector on auto generated columns"

WPF Grid Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  ]