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

Deselecting Selected Items

5 Answers 119 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Glenn
Top achievements
Rank 1
Glenn asked on 14 May 2010, 06:48 PM
I have a RadTreeView where the SelectionMode = "Multiple".  However, I only want nodes to remain selected if they are on the same level.  For instance, if two nodes are selected on level 1 and a node on level 2 is selected, I want the two nodes on level 1 to be deselected and the only node to be selected is the node on level 2.

I read this Knowledge Base Article and implemented the ContainerBindingCollection.  However, when I set the Selected property of the object in the SelectedItems collection of the Tree, the selected nodes still remain selected.

Below is the markup for the xaml:
        <telerik:ContainerBindingCollection x:Name="BindingsCollection">  
            <telerik:ContainerBinding PropertyName="IsSelected" Binding="{Binding Selected, Mode=TwoWay}" /> 
            <telerik:ContainerBinding PropertyName="IsExpanded" Binding="{Binding Expanded, Mode=TwoWay}" /> 
        </telerik:ContainerBindingCollection> 
 
        <telerik:HierarchicalDataTemplate    
            x:Key="UserTemplate" 
            telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">  
            <StackPanel Orientation="Horizontal">  
                <Image Source="Assets/Images/user.png"/>  
                <TextBlock Margin="5,0,0,0"  Text="{Binding UserName}" /> 
            </StackPanel> 
        </telerik:HierarchicalDataTemplate> 
          
        <telerik:HierarchicalDataTemplate   
            x:Key="MembershipTemplate"   
            ItemTemplate="{StaticResource UserTemplate}" 
            ItemsSource="{Binding User}" 
            telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}" > 
            <StackPanel Orientation="Horizontal">  
                <Image Source="Assets/Images/pkg.png"/>  
                <TextBlock Margin="5,0,0,0"  Text="{Binding Name}" /> 
            </StackPanel> 
        </telerik:HierarchicalDataTemplate> 
          
        <telerik:HierarchicalDataTemplate   
            x:Key="FacilityTemplate"   
            ItemTemplate="{StaticResource MembershipTemplate}" 
            ItemsSource="{Binding Membersip}" 
            telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">  
            <StackPanel Orientation="Horizontal">  
                <Image Source="Assets/Images/house.png"/>  
                <TextBlock Margin="5,0,0,0"  Text="{Binding Name}" /> 
            </StackPanel> 
        </telerik:HierarchicalDataTemplate> 
        <telerik:HierarchicalDataTemplate   
            x:Key="OrderTemplate"   
            ItemTemplate="{StaticResource FacilityTemplate}" 
            ItemsSource="{Binding Facility}" 
            telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">  
            <StackPanel Orientation="Horizontal">  
                <Image Source="Assets/Images/book.png"/>  
                <TextBlock Margin="5,0,0,0"  Text="Order: " /><TextBlock Text="{Binding OrderNumber}" /> 
            </StackPanel> 
        </telerik:HierarchicalDataTemplate> 
 


Below is how I spin throw the SelectedItems collection and set the Selected property to false:
            foreach (var obj in this.RadTreeView1.SelectedItems) {  
                if (!obj.GetType().Equals(item.Header.GetType())) {  
                    ((MyModelBase)obj).Selected = false;  
                }  
            }  
 

How can I deselect selected nodes?

Thanks,
Glenn

5 Answers, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 20 May 2010, 09:28 AM
Hi Glenn,

Please accept my apology for the delayed response.

The best way to accomplish your scenario in Silverlight is in the ViewModel classes or the business classes. I prepared a sample project illustrating this approach.

However, if you prefer to spin through the SelectedItems collection you can use the PreviewSelected event handler of the RadTreeView:
private void treeView_PreviewSelected(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
    RadTreeViewItem item = e.Source as RadTreeViewItem;
    foreach (var obj in this.treeView.SelectedItems)
    {
        if (!obj.GetType().Equals(item.Header.GetType()))
        {
            if (obj is Company)
                ((Company)obj).IsSelected = false;
            else if (obj is Department)
                ((Department)obj).IsSelected = false;
            else
                ((Employee)obj).IsSelected = false;
        }
    }
}

Please keep in mind that the business classes need to implement INotifyPropertyChanged.

Take a look at the project and let me know if it works for you or if you need more info.

Sincerely yours,
Tina Stancheva
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
Glenn
Top achievements
Rank 1
answered on 21 May 2010, 12:29 PM
Thank you very much Tina for your reply and sample code.

I implement INotifyPropertyChanged interface in my business objects and raise the PropertyChange event as you suggested.  I loop through the SelectedItems collection in the PreviewSelected event and it works perfectly.

Thanks,
Glenn

0
Accepted
Tina Stancheva
Telerik team
answered on 21 May 2010, 12:30 PM
Hi Glenn,

I am glad that I was able to help.

Please let us know if we can further assit you.

Greetings,
Tina Stancheva
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
Madhu
Top achievements
Rank 1
answered on 06 Sep 2010, 06:57 PM
Hi Tina,

Can the same thing be achieved only using XAML? I mean by simple databinding instead of writing code for the treeview event?

Writing a data trigger to retrieve a boolean value from the view model and using that to assign the 'IsSelected' property of the RadTreeViewItem to false.

If the above is possible, kindly provide a sample.

A scenario that would call for this is where there would be a need to restrict selecting a parent item along with a child item. In other words, allowing either only multiple parent items or multiple child items to get selected.

Warm Regards
Madhu
0
Hristo
Telerik team
answered on 09 Sep 2010, 04:49 PM
Hi Madhu,

The best way to implement your custom logic is to put it into the view model. I suppose you do not want to "pollute" the code behind with unnecessary event handlers? So, using PreviewSelected event handler can be avoided by placing the logic inside view model object. The project sent by my colleague Tina shows how to do it.

If you want to use only xaml to accomplish the scenario, I'm afraid it is not possible. Or at least it is not easy and straightforward.

Hope this helps. Please let us know if you need more info.

Kind regards,
Hristo Milyakov
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
Glenn
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Glenn
Top achievements
Rank 1
Madhu
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or