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

Setting cell style

2 Answers 499 Views
GridView
This is a migrated thread and some comments may be shown as answers.
CSL
Top achievements
Rank 1
CSL asked on 28 Oct 2016, 06:07 AM
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 Answers, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 28 Oct 2016, 11:58 AM
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!
0
CSL
Top achievements
Rank 1
answered on 01 Nov 2016, 02:39 PM
Thanks for the supporting articles.
Tags
GridView
Asked by
CSL
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
CSL
Top achievements
Rank 1
Share this question
or