Disable Selection Of Certain Nodes

2 posts, 0 answers
  1. heavywoody
    heavywoody avatar
    79 posts
    Member since:
    Oct 2009

    Posted 09 Sep 2010 Link to this post

    I am using a bound treeview that has two different hierartcial data templates.  Both need to be able to expand, but on one I don't want the user to be able to select that treeviewitem.   Only expand it.  Is that possible?  Also, is there a way to disable the expander on a treeviewitem as well if I know beforehand they have no subitems?  I am using LoadOnDemand.

    Here are my data templates

     <HierarchicalDataTemplate  DataType="{x:Type  Classes:Shmoog}" 
       ItemsSource="{Binding Converter={StaticResource childConverter}}">
            <StackPanel Orientation="Horizontal">
                <Image Source="/Images/DocumentIcon.png" Margin="0 0 3 0"></Image>
                    <TextBlock Foreground="White" Text="{Binding DisplayName}" />
            </StackPanel>
        </HierarchicalDataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type Model:WorkFlowTemplateItem}" 
       ItemsSource="{Binding Converter={StaticResource childConverter}}">
            <StackPanel Orientation="Horizontal">
                <Image Source="/Images/DropboxIcon.png" Margin="0 0 3 0"></Image>
                    <TextBlock Foreground="White" Text="{Binding DisplayName}" />
                 </StackPanel>
        </HierarchicalDataTemplate>

  2. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 15 Sep 2010 Link to this post

    Hello Christian,

    "Both need to be able to expand, but on one I don't want the user to be able to select that treeviewitem."

    You can add a bool property to your ViewModel item (ex: IsSelectable) and subscribe for the PreviewSelected event of RadTreeView and handle the event if IsSelectable is false.

    public class DataItem : INotifyPropertyChanged
    {
        public string DisplayName { get; set; }
        public bool IsSelectable { get; set; }
    }

    private void RadTreeView_PreviewSelected(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        DataItem dataItem = e.OriginalSource as DataItem;
        e.Handled = !dataItem.IsSelectable;
    }

    Also, is there a way to disable the expander on a treeviewitem as well if I know beforehand they have no subitems?

    There are 2 approaches for this to work.
    1. You can set IsEnabled to false to the whole RadTreeViewItem thus disabling the expander element as well.
    2. Using find the expander element in the visual tree and disable it directly:

    private void DisableExpander_Click(object sender, RoutedEventArgs e)
    {
        if(this.treeView1.SelectedItem == null)
        {
            return;
        }
     
        RadTreeViewItem selectedItem = this.treeView1.ContainerFromItemRecursive(this.treeView1.SelectedItem);
     
        if(selectedItem != null)
        {
            ToggleButton expander = selectedItem.ChildrenOfType<ToggleButton>().FirstOrDefault();
            if(expander != null)
            {
                expander.IsEnabled = !expander.IsEnabled;
            }
        }
    }

    I've attached my test project for further reference. Have a look at it and let me know if you have further questions or comments regarding this topic.

    Best wishes,
    Kiril Stanoev
    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. UI for WPF is Visual Studio 2017 Ready
Back to Top