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

RadTreeView with ControlTemplate causes crash when MouseWheel

2 Answers 93 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Haz Parakrama
Top achievements
Rank 1
Haz Parakrama asked on 04 Sep 2009, 05:46 PM
Hey Telerik,

So I'm trying to create a simple ControlTemplate for the RadTreeView so I can combine the RadTreeView's hierarchy checkbox functionality (with IsTriStateMode and IsOptionElementsEnabled) with a custom ItemTemplate for each item bound to the tree.  Basically I want a RadTreeView with a root node that has a checkbox and the text "ALL", and give that node children of RadTreeViewItems.  My goal is to make it a single control, so fellow developers can just attach the style and desired ItemTemplate and roll with it (the idea is so this control could be used if the backing object needs to have its state changed when its corresponding node is checked or unchecked [obviously this needs subscribing to the event handlers as well, but that's irrelevant here]).

The items get bound just fine and the checkboxes work and all as expected, but mousewheel-scrolling past the boundaries (at the top of the tree or at the bottom) throws an exception on the page:

"System.NullReferenceException: Object reference not set to an instance of an object
at Telerik.Windows.Controls.RadTreeView.OnRootVisualMouseWheel
(Oject sender, MouseWheelEventArgs e)"

My XAML is as follows:

<UserControl x:Class="Haz.Silverlight.TestProject.Page" 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" 
  xmlns:rad="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
  Width="400" Height="300"
   
   
  <UserControl.Resources> 
    <telerik:HierarchicalDataTemplate x:Key="TreeView"
      <TextBlock Text="{Binding Text, Mode=OneWay}"/> 
    </telerik:HierarchicalDataTemplate> 
 
    <Style x:Name="RadTreeViewStyle" TargetType="rad:RadTreeView"
      <Setter Property="Template"
        <Setter.Value> 
          <ControlTemplate TargetType="rad:RadTreeView"
            <rad:RadTreeView  
              MinHeight="{TemplateBinding MinHeight}" 
              MinWidth="{TemplateBinding MinWidth}" 
              Width="{TemplateBinding Width}" 
              VerticalAlignment="{TemplateBinding VerticalAlignment}" 
              HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
              IsTriStateMode="{TemplateBinding IsTriStateMode}" 
              IsOptionElementsEnabled="{TemplateBinding IsOptionElementsEnabled}"
 
              <rad:RadTreeViewItem Header="ALL" CheckState="On" IsExpanded="True" ItemsSource="{TemplateBinding ItemsSource}" ItemTemplate="{TemplateBinding ItemTemplate}" /> 
 
            </rad:RadTreeView > 
          </ControlTemplate> 
        </Setter.Value> 
      </Setter> 
    </Style> 
  </UserControl.Resources> 
 
  <Grid x:Name="LayoutRoot" Background="White"
 
    <rad:RadTreeView x:Name="tree" Style="{StaticResource RadTreeViewStyle}" ItemTemplate="{StaticResource TreeView}" Height="100" IsTriStateMode="True" IsOptionElementsEnabled="True" /> 
     
  </Grid> 
</UserControl> 
 

And my code-behind:
using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Windows.Data; 
 
namespace Haz.Silverlight.TestProject 
    public partial class Page : UserControl 
    { 
        private List<Data> list = new List<Data>(); 
 
        public Page() 
        { 
            InitializeComponent(); 
            Populate(); 
        } 
 
        public void Populate() 
        { 
            for (int i = 0; i < 10; i++) 
            { 
                list.Add(new Data("ITEM " + i.ToString())); 
            } 
 
            this.tree.ItemsSource = list; 
        } 
    } 
 
    public class Data 
    { 
        private string _text; 
        public string Text 
        { 
            get { return _text; } 
            set { _text = value; } 
        } 
 
        public Data(string text) 
        { 
            Text = text; 
        } 
    } 
 

I know I must be missing something, because if I DON'T set the ControlTemplate and structure my XAML as follows:

  <Grid x:Name="LayoutRoot" Background="White"
 
    <rad:RadTreeView Height="100" IsTriStateMode="True" IsOptionElementsEnabled="True"
      <rad:RadTreeViewItem  x:Name="tree" Header="ALL" CheckState="On" IsExpanded="True" ItemTemplate="{StaticResource TreeView}"/> 
    </rad:RadTreeView> 
     
  </Grid> 

everything works fine.  Am I missing a property or something?

This isn't urgent so deal with more pressing posts if you have them.  I'd appreciate any help though.

Thanks!

-Haz-

2 Answers, 1 is accepted

Sort by
0
Accepted
Valentin.Stoychev
Telerik team
answered on 07 Sep 2009, 08:43 AM
Hello Haz Parakrama,

you will need to define a scrollviewer inside the control template of the RadTreeView and name it "ScrollViewer".

This should fix the issue you are experiencing.

Kind regards,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Haz Parakrama
Top achievements
Rank 1
answered on 08 Sep 2009, 02:58 PM
Valentin,

Perfect!  Thanks very much.

-Haz-
Tags
Menu
Asked by
Haz Parakrama
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
Haz Parakrama
Top achievements
Rank 1
Share this question
or