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

Disable Selection Of Certain Nodes

1 Answer 346 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
heavywoody
Top achievements
Rank 1
heavywoody asked on 09 Sep 2010, 07:20 PM
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>

1 Answer, 1 is accepted

Sort by
0
Kiril Stanoev
Telerik team
answered on 15 Sep 2010, 10:08 AM
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
Tags
TreeView
Asked by
heavywoody
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Share this question
or