Setting cell style

3 posts, 1 answers
  1. CSL
    CSL avatar
    4 posts
    Member since:
    Oct 2016

    Posted 28 Oct Link to this post

    HI, 
    I have a RadGridView which item source is bind to DynamicObject. How can I apply cell style selector to specific column of RadGridView. For your information, the column is generated dynamically.

    Thank you.
  2. Answer
    Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    371 posts

    Posted 28 Oct Link to this post

    Hello Allen,

    If I understood you correctly, you can handle RadGridView's AutoGeneratingColumn event and either build the style selector programmatically or use it from XAML:

    private void radGridView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
    {
        if (e.ItemPropertyInfo.Name == "StadiumCapacity")
        {
            var bigStadiumStyle = new Style() { TargetType = typeof(GridViewCell) };
            bigStadiumStyle.Setters.Add(new Setter() { Property = GridViewCell.BackgroundProperty, Value = new SolidColorBrush(Colors.Red) });
            bigStadiumStyle.Setters.Add(new Setter() { Property = GridViewCell.ForegroundProperty, Value = new SolidColorBrush(Colors.Yellow) });
     
            var smallStadiumStyle = new Style() { TargetType = typeof(GridViewCell) };
            smallStadiumStyle.Setters.Add(new Setter() { Property = GridViewCell.BackgroundProperty, Value = new SolidColorBrush(Colors.Yellow) });
            smallStadiumStyle.Setters.Add(new Setter() { Property = GridViewCell.ForegroundProperty, Value = new SolidColorBrush(Colors.Red) });
     
            e.Column.CellStyleSelector = new StadiumCapacityStyle()
            {
                BigStadiumStyle = bigStadiumStyle,
                SmallStadiumStyle = smallStadiumStyle
            };
        }
    }

    Here's the XAML approach:

    private void radGridView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
    {
        if (e.ItemPropertyInfo.Name == "StadiumCapacity")
        {
            e.Column.CellStyleSelector = (StadiumCapacityStyle) App.Current.Resources["stadiumCapacityStyle"];
        }
    }

    <Application.Resources>
        <my:StadiumCapacityStyle x:Key="stadiumCapacityStyle">
            <my:StadiumCapacityStyle.BigStadiumStyle>
                <Style TargetType="telerik:GridViewCell">
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="Foreground" Value="Yellow"/>
                </Style>
            </my:StadiumCapacityStyle.BigStadiumStyle>
            <my:StadiumCapacityStyle.SmallStadiumStyle>
                <Style TargetType="telerik:GridViewCell">
                    <Setter Property="Background" Value="Yellow" />
                    <Setter Property="Foreground" Value="Red"/>
                </Style>
            </my:StadiumCapacityStyle.SmallStadiumStyle>
        </my:StadiumCapacityStyle>
    </Application.Resources>

    Please also have a look at the CellStyleSelector and Column Events articles if you have not done so already.

    Do let me know if this works for you. If that is not the case, please provide more details regarding your exact scenario and I will gladly guide you further in finding a solution.

    Regards,
    Dilyan Traykov
    Telerik by Progress
    Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
  3. UI for WPF is Visual Studio 2017 Ready
  4. CSL
    CSL avatar
    4 posts
    Member since:
    Oct 2016

    Posted 01 Nov in reply to Dilyan Traykov Link to this post

    Thanks for the supporting articles.
Back to Top