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

Find control in ControlTemplate or set alternating background color

2 Answers 90 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mitch Newlin
Top achievements
Rank 1
Mitch Newlin asked on 16 Dec 2010, 07:38 PM
I'm trying to find a control in my datacolumn's control template at run-time, so I can set the background color.  What I had hoped to do is just set the gridviews alternating row background color, so I wouldn't have to do this, but it's not working.  If you could either tell me how to reference the border control name "ItemBorder" at run-time so I can set it manually, or tell me how to set the gridviews alternating row background color correctly, I would appreciate it. 

Here is the xaml for my RadGrid:

       
<telerik:RadGridView x:Name="gvApplications" RowIndicatorVisibility="Collapsed" ScrollViewer.HorizontalScrollBarVisibility="Hidden" AlternateRowBackground="#DDDDDD" AutoGenerateColumns="false" SelectionMode="Single" EditTriggers="None" ItemsSource="{Binding Applications}" RowDetailsVisibilityMode="Collapsed" ShowColumnHeaders="False" ShowGroupPanel="False" Width="575" HorizontalAlignment="Left"  >
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn>
                        <telerik:GridViewDataColumn.CellStyle>
                            <Style TargetType="telerik:GridViewCell">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="telerik:GridViewCell">
                                            <Border x:Name="ItemBorder" BorderBrush="#FFC2C2C2" BorderThickness="2, 1, 2, 1" Width="575" HorizontalAlignment="Left" Background="#DDDDDD" Padding="5" Height="101">
                                                <StackPanel HorizontalAlignment="left" Margin="0" Background="Transparent" Width="565">
                                                    <Grid Width="565" HorizontalAlignment="Left" Height="65" Background="Transparent">
                                                        <Grid.RowDefinitions>
                                                            <RowDefinition Height="0.385*"></RowDefinition>
                                                            <RowDefinition Height="5"></RowDefinition>
                                                            <RowDefinition Height="0.615*"></RowDefinition>
                                                        </Grid.RowDefinitions>
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="0.25*"></ColumnDefinition>
                                                            <ColumnDefinition Width="0.282*"></ColumnDefinition>
                                                            <ColumnDefinition Width="0.282*"></ColumnDefinition>
                                                            <ColumnDefinition Width="0.186*"></ColumnDefinition>
                                                        </Grid.ColumnDefinitions>
                                                        <TextBlock x:Name="tbStudentName" Text="{Binding Path=Student.FullName}" Grid.Column="0" Grid.Row="0"></TextBlock>
                                                        <TextBlock x:Name="tbAddress" Text="{Binding Path=Student.Address}" Grid.Column="0" Grid.Row="2" VerticalAlignment="Top" Height="34"/>
                                                        <TextBlock Text="Card #:" Grid.Column="1" Grid.Row="0" Margin="0,0,18,0"></TextBlock>
                                                        <TextBlock x:Name="tbCardNumber" Text="{Binding Path=Student.GiftCard.CardNumber}" Grid.Column="1" Grid.Row="0" Margin="44,0,8,0"></TextBlock>
                                                        <TextBlock Text="Amount:" Grid.Column="2" Grid.Row="0" Margin="8,0,-8,0"></TextBlock>
                                                        <TextBox x:Name="tbAmount" Text="{Binding Path=Funding.Amount}" Grid.Column="2" Grid.Row="0" RenderTransformOrigin="1.217,0.56" Margin="61,0,8,0"/>
                                                        <TextBox x:Name="tbComments" Grid.Row="2"  Grid.Column="1" Grid.ColumnSpan="2" Margin="68,0,8,3"></TextBox>
                                                        <Button Content="Fund" Click="btnFund_Clicked"  Grid.Column="3" Grid.Row="0" Margin="44,0,8,0" RenderTransformOrigin="0.95,0.48" CommandParameter="{Binding ApplicationID}" Command="{Binding FundCard}"></Button>
                                                        <TextBlock Text="Comments:" Grid.Row="2" Grid.Column="1" Margin="0,0,20,0" Height="15" VerticalAlignment="Top" d:LayoutOverrides="VerticalAlignment"/>
                                                    </Grid>
                                                    <Border BorderThickness="2" BorderBrush="#BFDF55" Height="24" Margin="0,0,5,0" Background="#F8FCDF">
                                                        <StackPanel Orientation="Horizontal">
                                                            <Image Source="../Images/Check.png" Height="16" Width="16" Margin="5,0,0,0"></Image>
                                                            <TextBlock x:Name="tbActionText" Text=""></TextBlock>
                                                        </StackPanel>
                                                    </Border>
                                                </StackPanel>
                                            </Border>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </telerik:GridViewDataColumn.CellStyle>
 
 
                    </telerik:GridViewDataColumn>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>

2 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 17 Dec 2010, 02:19 PM
Hello Mitch,

 
Why do you actually need to access the template of RadGridView? You need to specify the RadGridView's AlternatonCount property which represents the count of alternating items as well as an appropriate color for their background:


this.radGridView1.AlternateRowBackground=new SolidColorBrush(Colors.Red);
this.radGridView1.AlternationCount=2;

If you need any further assistance please let me know.

Kind regards,
Vanya Pavlova
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Mitch Newlin
Top achievements
Rank 1
answered on 17 Dec 2010, 03:46 PM
As I said, I didn't want to have to access the cell template, but the alternate row color was not working.  I didn't realize you had to explicitly set the alternation count value, that really seems like something that should have a default value and only be overridden through that property.  In any case, setting that fixes my problem of setting the alternating row background color.  However, for future reference, should I need to access a control in my template, how would I do that?
Tags
GridView
Asked by
Mitch Newlin
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Mitch Newlin
Top achievements
Rank 1
Share this question
or