set selected item with HierarchicalDataTemplate

2 posts, 1 answers
  1. Jonam
    Jonam avatar
    36 posts
    Member since:
    Jul 2010

    Posted 15 Sep 2010 Link to this post

    i have prepared an example. I have a radTreeView with hierarchicaldatatemplating. I want to set the selected item when i click on the button. This works when i have collapsed all the treenodes manually. But when the nodes are collapsed it doesn't work. Can somebody explain this behaviour?

    <UserControl x:Class="TestTreeView.MainPage"
        mc:Ignorable="d" 
                 xmlns:local="clr-namespace:TestTreeView"
                 d:DesignWidth="640" d:DesignHeight="480" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
        <UserControl.Resources>
      
      
      
      
            <local:CheckStateConverter x:Key="CheckStateConverter"/>
            <!--Bindings collection for the RadTreeViewItems-->
            <telerik:ContainerBindingCollection x:Name="BindingsCollection">
                <telerik:ContainerBinding PropertyName="CheckState" Binding="{Binding IsSelected, Mode=TwoWay, Converter={StaticResource CheckStateConverter}}"/>
                <telerik:ContainerBinding PropertyName="IsExpanded" Binding="{Binding IsExpanded, Mode=TwoWay}"/>
            </telerik:ContainerBindingCollection>
      
            <DataTemplate x:Key="tBill" telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>
            <telerik:HierarchicalDataTemplate x:Key="tTopLevel" ItemsSource="{Binding Childs}" telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </telerik:HierarchicalDataTemplate>
            <telerik:HierarchicalDataTemplate x:Key="tLevel1" ItemsSource="{Binding Childs}" telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </telerik:HierarchicalDataTemplate>
            <telerik:HierarchicalDataTemplate x:Key="tLevel2" ItemsSource="{Binding Childs}" telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </telerik:HierarchicalDataTemplate>
            <telerik:HierarchicalDataTemplate x:Key="tLevel3" ItemsSource="{Binding Childs}" telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </telerik:HierarchicalDataTemplate>
            <telerik:HierarchicalDataTemplate x:Key="tLevel4" ItemsSource="{Binding Childs}" telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </telerik:HierarchicalDataTemplate>
            <telerik:HierarchicalDataTemplate x:Key="tLevel5" ItemsSource="{Binding Childs}" telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </telerik:HierarchicalDataTemplate>
            <telerik:HierarchicalDataTemplate x:Key="tLevel6" ItemsSource="{Binding Childs}" telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </telerik:HierarchicalDataTemplate>
            <telerik:HierarchicalDataTemplate x:Key="tContract" ItemsSource="{Binding Childs}" telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </telerik:HierarchicalDataTemplate>
            <local:AccountTemplateSelector x:Key="myDataTemplateSelector"
                    TopLevelTemplate="{StaticResource tTopLevel}"
                    AccountLevel1Template="{StaticResource tLevel1}"
                    AccountLevel2Template="{StaticResource tLevel2}"
                    AccountLevel3Template="{StaticResource tLevel3}"
                    AccountLevel4Template="{StaticResource tLevel4}"
                    AccountLevel5Template="{StaticResource tLevel5}"
                    AccountLevel6Template="{StaticResource tLevel6}"
                    ContractTemplate="{StaticResource tContract}"
                    BillTemplate="{StaticResource tBill}"/>
        </UserControl.Resources>
            <Grid x:Name="LayoutRoot">
            <Grid.RowDefinitions>
                <RowDefinition Height="432*" />
                <RowDefinition Height="48*" />
            </Grid.RowDefinitions>
            <telerik:RadTreeView 
                Name="radTreeView1"  
                Margin="5" 
                   
                IsLineEnabled="True"
                SelectionMode="Single" 
                IsEditable="False" 
                IsDragDropEnabled="False" 
                ItemTemplateSelector="{StaticResource myDataTemplateSelector}" >
      
            </telerik:RadTreeView>
            <Button Content="Button" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="39,13,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        </Grid>
    </UserControl>
    using System;
    using System.Collections.Generic;
    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 Telerik.Windows.Controls;
    using System.ComponentModel;
    using System.Windows.Automation;
    using System.Windows.Data;
      
    namespace TestTreeView
    {
        public partial class MainPage : UserControl
        {
      
            TreeAccountLevel5 level5a;
            public MainPage()
            {
                InitializeComponent();
      
                TreeTopLevel top = new TreeTopLevel();
                top.Id = 1;
                top.Name = "Top";
                top.Childs = new List<TreeAccountLevel1>();
      
      
      
                TreeAccountLevel1 level1a =  new TreeAccountLevel1();
                level1a.Id =  1  ;
                level1a.Name = "level1 - A";
                level1a.ParentId = 1;
                level1a.Childs = new List<TreeAccountLevel2>();
      
                TreeAccountLevel1 level1b = new TreeAccountLevel1();
                level1b.Id = 2;
                level1b.Name = "level1 - B";
                level1b.ParentId = 1;
                level1b.Childs = new List<TreeAccountLevel2>();
      
      
                TreeAccountLevel2 level2a = new TreeAccountLevel2();
                level2a.Id = 1;
                level2a.Name = "level2 - A";
                level2a.ParentId = 1;
                level2a.Childs = new List<TreeAccountLevel3>();
      
                TreeAccountLevel2 level2b = new TreeAccountLevel2();
                level2b.Id = 2;
                level2b.Name = "level2 - B";
                level2b.ParentId = 1;
                level2b.Childs = new List<TreeAccountLevel3>();
      
      
                TreeAccountLevel3 level3a = new TreeAccountLevel3();
                level3a.Id = 1;
                level3a.Name = "level3 - A";
                level3a.ParentId = 1;
                level3a.Childs = new List<TreeAccountLevel4>();
      
                TreeAccountLevel3 level3b = new TreeAccountLevel3();
                level3b.Id = 2;
                level3b.Name = "level3 - B";
                level3b.ParentId = 1;
                level3b.Childs = new List<TreeAccountLevel4>();
      
                TreeAccountLevel4 level4a = new TreeAccountLevel4();
                level4a.Id = 1;
                level4a.Name = "level4 - A";
                level4a.ParentId = 1;
                level4a.Childs = new List<TreeAccountLevel5>();
      
                TreeAccountLevel4 level4b = new TreeAccountLevel4();
                level4b.Id = 2;
                level4b.Name = "level4 - B";
                level4b.ParentId = 1;
                level4b.Childs = new List<TreeAccountLevel5>();
      
                level5a = new TreeAccountLevel5();
                level5a.Id = 1;
                level5a.Name = "level5 - A";
                level5a.ParentId = 1;
                level5a.Childs = new List<TreeAccountLevel6>();
      
                TreeAccountLevel5 level5b = new TreeAccountLevel5();
                level5b.Id = 2;
                level5b.Name = "level5 - B";
                level5b.ParentId = 1;
                level5b.Childs = new List<TreeAccountLevel6>();
      
                radTreeView1.Items.Add(top);
      
                top.Childs.Add(level1a);
                top.Childs.Add(level1b);
      
                level1a.Childs.Add(level2a);
                level1a.Childs.Add(level2b);
      
      
                level2a.Childs.Add(level3a);
                level2a.Childs.Add(level3b);
      
      
                level3a.Childs.Add(level4a);
                level3a.Childs.Add(level4b);
      
      
                level4a.Childs.Add(level5a);
                level4a.Childs.Add(level5b);
      
      
            }
      
            private void button1_Click(object sender, RoutedEventArgs e)
            {
      
                radTreeView1.SelectedItem = level5a;           
      
            }
      
      
        }
      
      
        public class AccountTemplateSelector : DataTemplateSelector
        {
            public AccountTemplateSelector()
            {
            }
      
            public Telerik.Windows.Controls.HierarchicalDataTemplate TopLevelTemplate
            {
                get;
                set;
            }
      
            public Telerik.Windows.Controls.HierarchicalDataTemplate AccountLevel1Template
            {
                get;
                set;
            }
      
            public Telerik.Windows.Controls.HierarchicalDataTemplate AccountLevel2Template
            {
                get;
                set;
            }
      
            public Telerik.Windows.Controls.HierarchicalDataTemplate AccountLevel3Template
            {
                get;
                set;
            }
      
      
            public Telerik.Windows.Controls.HierarchicalDataTemplate AccountLevel4Template
            {
                get;
                set;
            }
      
      
            public Telerik.Windows.Controls.HierarchicalDataTemplate AccountLevel5Template
            {
                get;
                set;
            }
      
      
            public Telerik.Windows.Controls.HierarchicalDataTemplate AccountLevel6Template
            {
                get;
                set;
            }
      
      
            public Telerik.Windows.Controls.HierarchicalDataTemplate ContractTemplate
            {
                get;
                set;
            }
      
      
            public DataTemplate BillTemplate
            {
                get;
                set;
            }
      
      
      
            public override DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                base.SelectTemplate(item, container);
      
                if (item is TreeAccountLevel1)
                {
                    return this.AccountLevel1Template;
                }
                else if (item is TreeTopLevel)
                {
                    return this.TopLevelTemplate;
                }
                else if (item is TreeAccountLevel2)
                {
                    return this.AccountLevel2Template;
                }
                else if (item is TreeAccountLevel3)
                {
                    return this.AccountLevel2Template;
                }
                else if (item is TreeAccountLevel4)
                {
                    return this.AccountLevel2Template;
                }
                else if (item is TreeAccountLevel5)
                {
                    return this.AccountLevel2Template;
                }
                else if (item is TreeAccountLevel6)
                {
                    return this.AccountLevel6Template;
                }
                else if (item is TreeContract)
                {
                    return this.ContractTemplate;
                }
                else if (item is TreeBill)
                {
                    return this.BillTemplate;
                }
                return null;
            }
        }
      
        public class CheckStateConverter : IValueConverter
        {
      
            #region IValueConverter Members
      
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                bool? result = (bool?)value;
                if (result.HasValue)
                {
                    return result.Value ? ToggleState.On : ToggleState.Off;
                }
                else return ToggleState.Indeterminate;
            }
      
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                ToggleState state = (ToggleState)value;
                if (state == ToggleState.Indeterminate)
                    return null;
                else return state == ToggleState.On ? true : false;
            }
      
            #endregion
        }
      
        public class TreeBaseItem : INotifyPropertyChanged
        {
            public int Id { get; set; }
            public string Name { get; set; }
      
      
            private bool isExpanded;
            public bool IsExpanded
            {
                get
                {
                    return isExpanded;
                }
                set
                {
                    if (value != isExpanded)
                    {
                        isExpanded = value;
                        OnPropertyChanged("IsExpanded");
                    }
                }
            }
            public bool? IsSelected { get; set; }
      
            #region INotifyPropertyChanged Members
      
            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
      
            public event PropertyChangedEventHandler PropertyChanged;
      
            #endregion
        }
      
        public class TreeTopLevel : TreeBaseItem
        {
      
            public List<TreeAccountLevel1> Childs { get; set; }
        }
      
      
        public class TreeAccountLevel1 : TreeBaseItem
        {
      
            public int ParentId { get; set; }
            public List<TreeAccountLevel2> Childs { get; set; }
      
        }
      
        public class TreeAccountLevel2 : TreeBaseItem
        {
      
            public int ParentId { get; set; }
            public List<TreeAccountLevel3> Childs { get; set; }
        }
      
        public class TreeAccountLevel3 : TreeBaseItem
        {
      
            public int ParentId { get; set; }
            public List<TreeAccountLevel4> Childs { get; set; }
        }
      
        public class TreeAccountLevel4 : TreeBaseItem
        {
      
            public int ParentId { get; set; }
            public List<TreeAccountLevel5> Childs { get; set; }
        }
      
        public class TreeAccountLevel5 : TreeBaseItem
        {
      
            public int ParentId { get; set; }
            public List<TreeAccountLevel6> Childs { get; set; }
        }
      
        public class TreeAccountLevel6 : TreeBaseItem
        {
      
            public int ParentId { get; set; }
            public List<TreeContract> Childs { get; set; }
        }
      
        public class TreeBill : TreeBaseItem
        {
      
            public int ParentId { get; set; }
      
        }
      
        public class TreeContract : TreeBaseItem
        {
            public int ParentId { get; set; }
            public List<TreeBill> Childs { get; set; }
        }
    }
  2. Answer
    Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 20 Sep 2010 Link to this post

    Hello Jonam,

    Actually the item will be selected when is shown. If you mark the item as Selected from the code behind and expand all his parents it should be displayed as Selected.

    The tree view is quite virtualized meaning that items are created when they are required. That is why the item container (RadTreeViewItem) is not created if the item is not required in some scenario. When the item is going to be displayed its container is created. So marking a business item as Selected will not take visual effect until the item has been displayed and its container had been created.

    In order to force creation of the required item you can expand its parents. If the tree is quite big the user should scroll to the particular area and the item shall be selected. Following code snippet demonstrates what I mean:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        top.IsExpanded = true;
        top.Childs[0].IsExpanded = true;
        top.Childs[0].Childs[0].IsExpanded = true;
        top.Childs[0].Childs[0].Childs[0].IsExpanded = true;
        top.Childs[0].Childs[0].Childs[0].Childs[0].IsExpanded = true;
        top.Childs[0].Childs[0].Childs[0].Childs[0].Childs[0].IsExpanded = true;
        radTreeView1.SelectedItem = level5a;
    }

    Hope this helps. Please let us know if you need more help.


    Sincerely yours,
    Hristo Milyakov
    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
  3. DevCraft banner
Back to Top