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

Deselect items from a MultiSelect Treeview

10 Answers 355 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 13 Nov 2010, 04:37 AM
Hey guys!

I have been having trouble getting my RadTreeView to deselect items that I have selected with multiselect.
What am I missing?

You can check out my sample project here
http://www.vbninja.com/TreeViewMultiSelect2.zip

Thanks
Scott

10 Answers, 1 is accepted

Sort by
0
Scott
Top achievements
Rank 1
answered on 16 Nov 2010, 06:54 PM
Guys,

Why do WPF questions take so long to answer?  

Whenever I post a question in the ASP.Net forums, my questions are answered within a day and usually within a couple of hours.  When I post a WPF question, the usual response time is a week to answer.
0
Petar Mladenov
Telerik team
answered on 17 Nov 2010, 02:57 PM
Hi Scott,

Basically, the Unselected event is fired for every unselected RadTreeViewItem - for each item that you have previously selected in your multiselection. Also the RadTreeView.SelectedItems collection at all times contains the currently selected items and when the RadTreeView is databound its items are of the type of the business objects.You can find more info about the RadTreeView Selection feature here.

This means that the DeHighlightItem() method has to be modified since the SelectedItems collection contains only one element - the newly selected item (the business object  represented by the container you have clicked last - selectedItem.Item == selectedItem.ParentTreeView.SelectedItems[0] ). So you can just try this:
private void DeHighlightItem(RadTreeViewItem selectedItem)
       {
           selectedItem.Background = (System.Windows.Media.Brush)FindResource("ListItemUnSelect");
       }

Please examine the described approach in the attached file. Still, please keep in mind that the default behavior of the Unselected and Selected events, should work for you as well. Please let me know if this is what you expected or if I am missing something. You can

Also, we deeply apologize for being behind the schedule sometimes. We always try to reply within the 72- hours response time in the forums. We will be definitely trying to avoid this in the future.

Best wishes,
Petar Mladenov
the Telerik team
See What's New in RadControls for WPF in Q3 2010 on Tuesday, November 16, 2010 11:00 AM - 12:00 PM EST or 10:00 PM - 11:00 PM EST: Register here>>
0
Scott
Top achievements
Rank 1
answered on 17 Nov 2010, 03:19 PM
Thanks for the reply.

I think I am explaining what I am wanting incorrectly.

I want to be able to select multiple items in the Treeview by clicking on the list items.  Reclicking on an already selected Treeview item will deselect it.

I have included a picture to show there are multiple items selected at once.  
The current example provided only allows me to click and select one item at a time.

Thanks
Scott
0
Petar Mladenov
Telerik team
answered on 22 Nov 2010, 06:53 PM
Hello Scott,

Unfortunately the RadTreeView doesn't support this functionality for now. However, we have this feature in our to-do list and you can vote for oit in our PITS thus increasing its priority.

In the meantime there is a possible workaround, although it isn't very subtle. Can you please
examine the attached project and let me know if it works for you?

Please feel free to ask if you need further assistance. We would be glad to help you.

Sincerely yours,
Petar Mladenov
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Scott
Top achievements
Rank 1
answered on 22 Nov 2010, 07:45 PM
Petar,

That almost works for us.  Only 2 problems that I notice.

1)  rt_PreviewUnselected is not a generic solution and only works in this case for the Family class.
2) bug : Select item 1, deselect item 1, reselect item 1.  Now select item 2.  Item 1 is now deselected.
This happenes for any item in the list.


Thanks
Scott
0
Petar Mladenov
Telerik team
answered on 25 Nov 2010, 11:06 AM
Hello Scott,

In order to implement generic logic in your solution, you can create a FamilyBase class to be a parent class of the Member and Family. For instance, you can define a boolean property Selected and use it for binding to the IsSelected property of the RadTreeViewItems (http://www.telerik.com/help/wpf/radtreeview-how-to-bind-selected-item.html).

Please examine the attached solution where the selection bug is fixed and the FamilyBase is defined (without the mentioned binding). Let us know if it satisfies you or not. We would be glad to assist you further.

Regards,
Petar Mladenov
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Ryan Black
Top achievements
Rank 1
answered on 29 Nov 2010, 05:31 PM
That worked great!

I thought I would be able to figure out this last part by myself but seem to have hit a roadblock.  I want to have the selected items Foreground color be set to red when they are selected.  It would change back to the original color if they were deselected.

Do you mind altering the solution to show this change?

We are learning a lot from these examples.

Thanks
Ryan
0
Accepted
Petar Mladenov
Telerik team
answered on 02 Dec 2010, 03:56 PM
Hello Ryan Black,

In order to achieve the desired functionality, we made some changes in your Generic.xaml. First, in the RadTreeViewMainItemControlTemplate, we changed the ContentPresenter  named "Header" to a ContentControl since the first doesn't provide a Foreground property. This way we were able to add new Setter in the trigger that fires when the IsSelected property of a RadTreeViewItem is true:
<Trigger Property="IsSelected" Value="True">
              <Setter Property="Visibility" TargetName="SelectionVisual" Value="Visible"/>
              <Setter Property="BorderThickness" Value="1" />
              <Setter Property="Foreground" TargetName="Header" Value="Red"/>
          </Trigger>
We also removed the Label element from the HierarchicaDataTemplate since it doesn't allow changing its foreground through the above trigger. Please have a look at the attached sample and let us know if this implementation of the HierarchicaDataTemplate works for you.

On the other hand, you can implement this scenario from code behind. This approach is also illustrated in the attached sample - the commented code in the Selected event handler. However, it`s better to use XAML (the first approach) when you are able to.

All the best,
Petar Mladenov
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Ryan Black
Top achievements
Rank 1
answered on 08 Dec 2010, 10:05 PM
Petar,

We have implemented this sample into our code and are now trying to deselect the selected items.

I created a deselect button and added the following code, but nothing seems to happen but the treeview list scrolling.

for (int i = 0; i < Treeview.ChildrenOfType<RadTreeView>()[0].Items.Count; i++)
            {
                // try to get the item Container 
                RadTreeViewItem childItemContainer = Treeview.ChildrenOfType<RadTreeView>()[0].ItemContainerGenerator.ContainerFromIndex(i) as RadTreeViewItem;
                if (childItemContainer != null)
                {
                    // DeSelect Item in list
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        childItemContainer.IsSelected = false;
                        childItemContainer.BringIntoView();
                        childItemContainer.Focus();
                        childItemContainer.UpdateLayout();
                    }), System.Windows.Threading.DispatcherPriority.ApplicationIdle);
                }
            }

I updated our sample back to Treeview MultiSelect Example if that is easier to look at.

Thanks
Ryan
0
Petar Mladenov
Telerik team
answered on 14 Dec 2010, 10:45 AM
Hi Ryan Black,

When you are setting container.IsSelected = false, the PreviewUnselected event of the RadTreeView fires. But you have missed to set the deselectionRealTarget which is needed  to make the tree deselect normally. In addition , I added a public property DeselectionTarget to wrap your private field deselectionRealTarget. You can find the described approach in the attached solution. Please let us know if it satisfies you or not.

Regards,
Petar Mladenov
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
Tags
TreeView
Asked by
Scott
Top achievements
Rank 1
Answers by
Scott
Top achievements
Rank 1
Petar Mladenov
Telerik team
Ryan Black
Top achievements
Rank 1
Share this question
or