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

How to apply alternate row style

3 Answers 194 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Ketaki
Top achievements
Rank 1
Ketaki asked on 27 May 2009, 01:36 PM
Hi,

Can we apply alternate row style in RadTreeView similary to RadGridView.
I can see a  properlty in RadGridView with AlternateRowStyle which applies alternate row styling to the grid data.
Is there any such support for RadTreeView items as well?
If yes, can you give me guildline to achieve it?

Thanks
Ketaki

3 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 29 May 2009, 01:19 PM
Hello Ketaki,

There is no built-in support for the alternating style, but you can achieve a similar effect with a StyleSelector.

The TreeView has a ItemContainerStyleSelector property which will apply custom styles to all its containers (TreeViewItems).

What you can do is have two styles (alternate and normal) and return them alternatingly.

You can create your own style selector by inheriting the StyleSelector class and implementing the SelectStyle() method. There you get the respective item and its TreeViewItem container. There you can check for the index of the TreeViewItem and return the appropriate style.

Unfortunately this will work initially but if the items are rearranged or otherwise changed, the alternation will be lost. The StyleSelector will select a style just once, when the item is created.

Please come back to us if you need help implementing this.

All the best,
Dimitrina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Kurt Nelezen
Top achievements
Rank 1
answered on 12 Mar 2010, 04:57 PM
I have created a style selector.  I am trying to use the selelctor to create alternating brush for the RadTreeViewItem, but I am not sure what is wrong.  When I run the application, it throws an exception.  I am using Version Q3 2009 SP2 of the Rad Controls for Silverlight.  Here is an example of my xaml.

    <navigation:Page.Resources> 
          
        <CSUICommon:AlternatingStyleSelector x:Key="LinkItemStyleSelector" /> 

    <navigation:Page.Resources> 
                 
...  
 
    <telerikNavigation:RadTreeView x:Name="tvwLinks" 
                                   Margin="0,0,0,0"   
                                   ItemContainerStyleSelector="{StaticResource LinkItemStyleSelector}"
                                  
Style="{StaticResource PlusMinusTreeViewStyle}"   
                                   PathSeparator="|"   
                                   IsDragPreviewEnabled="False"   
                                   IsDragTooltipEnabled="False"   
                                   IsTextSearchEnabled="False">  
        <telerikNavigation:RadTreeView.ItemTemplate> 
            <telerik:HierarchicalDataTemplate ItemsSource="{Binding Links}" 
                                              ItemTemplate="{StaticResource LinksChildren}">  
                 <TextBlock Text="{Binding Display}" /> 
            </telerik:HierarchicalDataTemplate> 
        </telerikNavigation:RadTreeView.ItemTemplate> 
    </telerikNavigation:RadTreeView> 
 

and this is my Style Selector Class.

Imports Telerik.Windows.Controls  
 
Public Class AlternatingStyleSelector  
    Inherits StyleSelector  
 
    Public Overrides Function SelectStyle(ByVal item As ObjectByVal container As System.Windows.DependencyObject) As System.Windows.Style  
        Dim tvwTreeView As RadTreeView = ItemsControl.ItemsControlFromItemContainer(container)  
        Dim intIndex As Integer = tvwTreeView.ItemContainerGenerator.IndexFromContainer(container)  
        Dim objStyle As New Style  
        Dim objBackgroundSetter As New Setter  
 
        objStyle.TargetType = GetType(RadTreeViewItem)  
 
        If intIndex Mod 2 = 0 Then 
            objBackgroundSetter.Property = RadTreeViewItem.BackgroundProperty  
            objBackgroundSetter.Value = Color.FromArgb(&HFF, &HBA, &HDE, &HEC)  
        Else 
            objBackgroundSetter.Property = RadTreeViewItem.BackgroundProperty  
            objBackgroundSetter.Value = Color.FromArgb(&HFF, &HFF, &HFF, &HFF)  
        End If 
 
        objStyle.Setters.Add(objBackgroundSetter)  
 
        Return objStyle  
 
    End Function 
 
End Class 
 

The exception is thrown as the SelectStyle method is returning the new style object.
The exception is a Managed Runtime Exception and the message is:  "Value does not fall within the expected range." with error code: 4004.

Please help.
0
Vladislav
Telerik team
answered on 17 Mar 2010, 12:29 PM
Hi Kurt Nelezen,

I have prepared a sample project using the StyleSelector.
I hope that it will help you with your task.

If you have any additional questions don't hesitate to ask.

All the best,
Vladislav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
Ketaki
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Kurt Nelezen
Top achievements
Rank 1
Vladislav
Telerik team
Share this question
or