Question

I want to take the highlight background off of the radtreeview. I created a style to do this, but I keep getting errors and exceptions such as "Items collection must be empty." If I comment out the style the application works fine, so I know that it is the cause of the problem. I am fairly new to WPF, and I am sure I just don't understand how to use styles yet. Thanks for your help. Here is the code.

<Grid x:Name="LayoutRoot" Background="Salmon">

        <telerik:RadTreeView x:Name="radTreeView" Margin="8" ItemsSource="{Binding Errors}" Background="Salmon" Style="{StaticResource treeStyle}">
             <Style TargetType="{x:Type telerik:RadTreeViewItem}" x:Name="treeStyle">
                <Setter Property="Focusable" Value="False"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" Value="{x:Null}"/>
                        <Setter Property="BorderBrush" Value="{x:Null}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>


            <telerik:RadTreeView.ItemTemplate>
                 <HierarchicalDataTemplate ItemsSource="{Binding SubItems}" >
                    <Grid Background="Salmon">
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>

                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding Description}" IsHitTestVisible="False" />

                        <ListBox Grid.Row="1" ItemsSource="{Binding Messages}" Margin="20,0,0,0" BorderBrush="#00000000" BorderThickness="0" Background="Salmon" IsHitTestVisible="False" >
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Message}"/>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid>

                </HierarchicalDataTemplate>


            </telerik:RadTreeView.ItemTemplate>


        </telerik:RadTreeView>

    </Grid>

</UserControl>

If you know that this is not going to work, I was also trying to get rid of the highlight with the style code:

<Style TargetType="TreeViewItem">
      <Style.Resources>
          <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFF"/>
          <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="#000"/>
     </Style.Resources>  
</Style>
Was it helpful?

Solution

You get exceptions because your style tag is actually an item in the tree, and you have ItemsSource set.

Surround the style with <telerik:RadTreeView.ItemContainerStyle> tag.

This should solve the exception but it will not give you the result that you expect since the control template of the tree view item is actually showing another border that is not affected by the Background property. You will need to change the control template.

Telerik change the styles between releases, so giving you a template of a wrong version will probably won't help you.

But, you can go to the installation folder for Telerik and look for a folder called "Themes". There you'll find a solution with all the themes for telerik.

  • Choose the one that you use.
  • Find the resource dictionary for the tree view and copy the style and template for the item to your project.
  • Change xmlns definitions, make sure you have all the brushes and resources that the style depends upon.
  • Run to see that the style is ok.
  • In the template, find the VisualState with x:Name="MouseOver" and delete the storyboard inside it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top