Master
Master

Reputation: 2153

Adding Border for GridViewCell Programmatically

I'm trying to add a Thick Border around a certain column programmatically. I can change the Background property using identical methods but the border property won't change in anyway.

if (eligibleProperties.Contains(column.Header.ToString()))
{
    Setter s1 = new Setter(GridViewCell.BorderThicknessProperty, new Thickness(7));
    Setter s2 = new Setter(BorderBrushProperty, Brushes.Black);
    Style st = new Style(typeof(GridViewCell));
    st.Setters.Add(s1);
    st.Setters.Add(s2);

    column.CellStyle = st;
}

Working code for Background change

if (eligibleProperties.Contains(column.Header.ToString()))
{
    Setter s2 = new Setter(BackgroundProperty, Brushes.Black);
    Style st = new Style(typeof(GridViewCell));
    st.Setters.Add(s2);

    column.CellStyle = st;
}

Upvotes: 1

Views: 1567

Answers (2)

Oceans
Oceans

Reputation: 3509

I believe your best bet is to change the template of the cells for that column, you can even do it on the fly by setting the telerik:GridViewDataColumn.CellStyle of the column to this:

<Style TargetType="{x:Type telerik:GridViewCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="telerik:GridViewCell">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                    <ContentPresenter  Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" ToolTip="{TemplateBinding ToolTip}" VerticalAlignment="Center" Margin="3,0"/> 
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Now your cells have the properties BorderThicknessand BorderBrush to work with. Like in this example below your cells will have a thick red border all around:

<telerik:GridViewDataColumn Header="MyColumn1" DataMemberBinding="{Binding MyColumn1Binding}" Width="150" >
    <telerik:GridViewDataColumn.CellStyle>
        <Style TargetType="{x:Type telerik:GridViewCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="telerik:GridViewCell">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                            <ContentPresenter  Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" ToolTip="{TemplateBinding ToolTip}" VerticalAlignment="Center" Margin="3,0"/> 
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Propert="BorderBrush" Value="Red" />
            <Setter Propert="BorderThickness" Value="2,2,2,2" />
        </Style>
    </telerik:GridViewDataColumn.CellStyle>
</telerik:GridViewDataColumn

Using the thickness you could for example also hide the top and bottom borders. In the style you can also make use of Style.Triggers and define different border settings based on multiple DataTriggers.

I believe this offers much more flexibility than adding the borders programmatically. However, if you really want to do this in code then you can still define the template in XAML as in my first example which should make the setters from your code have affect.
And if this still doesn't seem sufficient enough, defining the template of the cell completely programmatically would be your next best bet. If you need help to translate the XAML to actual code just leave a comment, although I believe doing it in XAML is a lot better.

Upvotes: 1

mm8
mm8

Reputation: 169220

Because of the default template structure of a Telerik GridViewCell, modifying the cell borders is actually more complicated than just setting the BorderThickness and BorderBrush properties.

You will need to modify the control template. Please refer to the following threads for more information about this.

How do I change border color in cell style selector: https://www.telerik.com/forums/how-do-i-change-border-color-in-cell-style-selector

Change the row border colour: https://www.telerik.com/forums/change-the-row-border-colour

Upvotes: 1

Related Questions