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

Prevent nodes from being selected

1 Answer 345 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
steven
Top achievements
Rank 1
steven asked on 06 Mar 2015, 02:18 PM
I have a RadTreeView bound in an MVVM style application. If a selection does not contain Details I need to disable the ability to click on that node and keep the currently selected node highlighted. The user should still able to expand and collapse.

I have tried to bind to PreviewSelected but the SelectedItem property still gets hit even if I switch e.handled = true and I lose the node highlighting on the currently selected node. 

<
UserControl.Resources>
    <ResourceDictionary>
        <Style x:Key="PHToCItemContainerStyle" TargetType="{x:Type telerik:RadTreeViewItem}" BasedOn="{StaticResource RadTreeViewDefaultItemPlusMinusStyle}">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
        </Style>
        <DataTemplate x:Key="Measure">
            <TextBlock Text="{Binding Name }" Style="{StaticResource RadTreeViewItemSelectedTextBlockDefaultStyle}" VerticalAlignment="Center"/>
        </DataTemplate>
        <HierarchicalDataTemplate x:Key="Condition" ItemTemplate="{StaticResource Measure}" ItemsSource="{Binding Measures}">
            <TextBlock Text="{Binding Name}" Style="{StaticResource RadTreeViewItemSelectedTextBlockDefaultStyle}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="ConditionGroup" ItemTemplate="{StaticResource Condition}" ItemsSource="{Binding Conditions}" >
            <TextBlock Text="{Binding Name }" FontWeight="Bold" Style="{StaticResource RadTreeViewItemSelectedTextBlockDefaultStyle}" />
        </HierarchicalDataTemplate>
    </ResourceDictionary>
</UserControl.Resources>
 
...
 
<telerik:RadTreeView
    x:Name="RadTree"     
    telerik:TextSearch.TextPath="Name"   
    VirtualizingStackPanel.IsVirtualizing="True"
    VirtualizingStackPanel.VirtualizationMode="Recycling"
    ItemsSource="{Binding ResultObject}"
    Style="{StaticResource RadTreeViewDefaultPlusMinusStyle}"
    ExpanderStyle="{StaticResource RadTreeViewDefaultPlusMinusExpanderStyle}"
    ItemTemplate="{StaticResource ConditionGroup}"
    ItemContainerStyle="{StaticResource PHToCItemContainerStyle}"
    SelectedItem="{Binding ResultObject.SelectedIndexItem}" />

dll version = 2014.1.331.45

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 11 Mar 2015, 09:39 AM
Hello Steven,

Indeed, when you try the cancel the selection in the PreviewSelection event the previously selection is removed. However, this is not reproducible with the latest version of our controls. Furthermore, we recommend using the PreviewSelectionChanged event instead of the PreviewSelection. If you handle the PreviewSelectionChanged event of the tree, the issue won't be observed.
private void RadTreeView_PreviewSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    e.Handled = true;
}

I hope this helps.

Regards,
Martin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
TreeView
Asked by
steven
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or