TreeView with only leaves selectable

7 posts, 0 answers
  1. Michele
    Michele avatar
    3 posts
    Member since:
    Mar 2011

    Posted 05 Apr 2011 Link to this post

    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>
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 07 Apr 2011 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Louis
    Louis avatar
    83 posts
    Member since:
    Aug 2013

    Posted 11 Aug 2015 Link to this post

    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

  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 13 Aug 2015 Link to this post

    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
  6. Louis
    Louis avatar
    83 posts
    Member since:
    Aug 2013

    Posted 13 Aug 2015 in reply to Petar Mladenov Link to this post

    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

  7. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 14 Aug 2015 Link to this post

    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
  8. Louis
    Louis avatar
    83 posts
    Member since:
    Aug 2013

    Posted 14 Aug 2015 in reply to Petar Mladenov Link to this post

    That works perfectly, thank you very much Petar!

    Louis

     

Back to Top
UI for WPF is Visual Studio 2017 Ready