Deselecting Selected Items

6 posts, 1 answers
  1. Glenn
    Glenn avatar
    12 posts
    Member since:
    Apr 2010

    Posted 14 May 2010 Link to this post

    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
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 20 May 2010 Link to this post

    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.
  3. DevCraft banner
  4. Glenn
    Glenn avatar
    12 posts
    Member since:
    Apr 2010

    Posted 21 May 2010 Link to this post

    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

  5. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 21 May 2010 Link to this post

    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.
  6. Madhu
    Madhu avatar
    9 posts
    Member since:
    Aug 2010

    Posted 06 Sep 2010 Link to this post

    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
  7. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 09 Sep 2010 Link to this post

    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
Back to Top
DevCraft banner