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

TreeView with only leaves selectable

6 Answers 541 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Michele
Top achievements
Rank 1
Michele asked on 05 Apr 2011, 04:33 PM
Hi, i have I need to use a treeview where you can select only leaf elements.
For my needs I created a treeview databinding with a HierarchicalDataTemplate.

I tried to disable the intermediate nodes, but this way I are also disabled child nodes.
How can I do?

This is the XAML

<UserControl.Resources>
        <Style x:Key="containerStyle" TargetType="{x:Type telerik:RadTreeViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelezionato, Mode=TwoWay}" />
            <Setter Property="IsExpanded" Value="{Binding IsEspanso, Mode=TwoWay}" />
            <Setter Property="IsEnabled" Value="{Binding IsAttivo, Mode=OneWay}" />
        </Style>
          
        <HierarchicalDataTemplate x:Key="BreadMenuCrumpTemplateL1"  ItemsSource="{Binding Children}" ItemContainerStyle="{StaticResource containerStyle}">
            <TextBlock Text="{Binding Titolo}" Margin="0" VerticalAlignment="Stretch" />
        </HierarchicalDataTemplate>
  
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot">
        <telerik:RadDropDownButton Content="{Binding TitoloElemento, Mode=OneWay}" Margin="0">
            <telerik:RadDropDownButton.DropDownContent>
                <telerik:RadTreeView x:Name="radTreeView" ItemsSource="{Binding TreeMenu.Children}"  ItemTemplate="{StaticResource BreadMenuCrumpTemplateL1}" IsExpandOnSingleClickEnabled="True" IsExpandOnDblClickEnabled="False" IsSingleExpandPath="True" IsDragPreviewEnabled="False" IsDragTooltipEnabled="False" BringIntoViewMode="HeaderAndItems"  SelectedItem="{Binding CurrentTreeItem, Mode=TwoWay}" IsEditable="False"/>                       
            </telerik:RadDropDownButton.DropDownContent>
        </telerik:RadDropDownButton>
    </Grid>

6 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 07 Apr 2011, 08:05 AM
Hello Michele,

You can use the RadTreeView.PreviewSelectionChanged event and to set e.handled = true if the item being clicked is not a leaf element ( its Container(RadTreeViewItem)`s Items collection is not empty). Feel free to ask if you need assistance on this.

Kind regards,
Petar Mladenov
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
0
Louis
Top achievements
Rank 1
answered on 11 Aug 2015, 07:29 PM

I've used the RadTreeView.PreviewSelectionChanged event to reject nodes in the tree that I don't want selectable, and while the selection doesn't change the solid box around an item still moves to the new item. Now it's in a situation where 3 items in the list are "highlighted" in different ways: the selected item in dark grey, the last clicked item with the box around it, and the current moused-over item in light gray (see attached screenshot). Is there a way to prevent the solid box from moving off the selected item as well?

Thanks,

Louis

0
Petar Mladenov
Telerik team
answered on 13 Aug 2015, 12:38 PM
Hi Louis,

We have similar issue reported for SIlverlight only and we are not aware of such focus-moving issue in WPF. Could you please elaborate a bit more on your scenario ? You can open a new support thread with an isolation. We would be glad to investigate it further.

Regards,
Petar Mladenov
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Louis
Top achievements
Rank 1
answered on 13 Aug 2015, 02:50 PM

The scenario is pretty much what was originally described. To illustrate, I took your "BringIntoView_WPF" example from the SDK, and added PreviewSelectionChanged="TreeSelectionPreview" to the RadTreeView:

 

private void TreeSelectionPreview(object sender, SelectionChangedEventArgs e)
{
    var item = e.AddedItems[0] as BusinessItem;
    var parts = item.Name.Split(new char[] {'.'});
    if (parts.Count() != 4)
    {
        e.Handled = true;
    }
}

Now, if you click 85.1.1.1 (successful select), then click 85.1 (not selectable), and mouse over 85.1.1, you get the attached picture. I don't want 85.1 to have a box around it; it wasn't selected, it has no special status.

 

Thanks,

Louis

0
Petar Mladenov
Telerik team
answered on 14 Aug 2015, 10:59 AM
Hello Louis,

Thank you for the additional information. The behavior you describe is expected. The Focus is changed on MouseLeftButtonDown, when you click 85.1, then the PreviewSelectionChanged fires and you are reverting the selection with e.Handler =true. However, there is no code in the RadTreeView responsible for reverting the focus in such scenario when you stop the selection. You need to do it manually. Here is our suggestion for sample code reverting the focus:

private void myTreeView_PreviewSelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          var item = e.AddedItems[0] as BusinessItem;
          var parts = item.Name.Split(new char[] { '.' });
          if (parts.Count() != 4)
          {
              e.Handled = true;
 
              var selected = this.myTreeView.SelectedContainer;
              if (selected != null)
                  selected.Focus();              
          }
      }


Regards,
Petar Mladenov
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Louis
Top achievements
Rank 1
answered on 14 Aug 2015, 01:57 PM

That works perfectly, thank you very much Petar!

Louis

 

Tags
TreeView
Asked by
Michele
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Louis
Top achievements
Rank 1
Share this question
or