Selection change prevention

7 posts, 0 answers
  1. Tal
    Tal avatar
    11 posts
    Member since:
    Jan 2009

    Posted 15 Feb 2009 Link to this post

    Hi guys,
    is there a way to prevent the selection change of nodes? I saw there is only a selectionchanged event which is not much of a help here since the selected node was already changed..

  2. Bobi
    Bobi avatar
    513 posts

    Posted 17 Feb 2009 Link to this post

    Hello Tal,

    You can use the PreviewSelected event to stop selection of an item. Check the code below:

    void private void RadTreeView1_PreviewSelected(object sender, RadRoutedEventArgs e)  
        // get a reference to the item that will be selected  
        RadTreeViewItem itemToSelect = e.Source as RadTreeViewItem;  
        // if the action should be canceled set Handled property to true  
        e.Handled = true;  

    Please let us know if you have any other questions!

    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. Tal
    Tal avatar
    11 posts
    Member since:
    Jan 2009

    Posted 17 Feb 2009 Link to this post

    well this way the Unselected event is still being thrown so my previous selection doesn't exist anymore. Anyway, I used the SelectionChanged event to validate and rollback in case the validation has failed.

  5. Ogi Ivanov
    Ogi Ivanov avatar
    8 posts
    Member since:
    Feb 2010

    Posted 23 Feb 2010 Link to this post

    Hi Broyana,
    I'm working on a WPF project using the MVVM pattern and I have two questions related to this post:
    1. Can I stop the next selection of a tree item using the ViewModel instead of handling the event in the code behind.
    2. How can I keep the selected brush on the previously selected item when the selection is stopped.

    Thank you,
    Ogi Ivanov
  6. Miroslav
    Miroslav avatar
    922 posts

    Posted 25 Feb 2010 Link to this post

    Hello Ogi Ivanov,

    1. Currently there is no way to stop the selection from the ViewModel. If you want to use databinding you may create a property in your view that will be databound to a property in your ViewModel that will specify whether the PreviewSelectionChanged event should be handled (this will stop the selection).

    The above is also true if you make this into an attached property or behavior and attach it where you want the selection to be handled.

    2. The item will be grayed-out if it is selected but the TreeView has no focus. Calling Focus() on the TreeView should keep it unchanged.

    Alternatively if you want to have the same visual look but keep the focus away from the TreeView you will need to edit the TreeViewItem's template. 

    Sincerely yours,
    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.
  7. Robert
    Robert avatar
    15 posts
    Member since:
    Mar 2013

    Posted 10 Apr 2013 Link to this post

    I was looking for something very similar, but I needed to get the actual RadTreeViewItem, as your code example showed. However e.Source actually returns the TreeView, not the item being unselected, and as you try to cast it as a RadTreeViewItem the itemToSelect would actually be null. Is there a way to actually get the item being selected/unselected in the event?
  8. Pavel R. Pavlov
    Pavel R. Pavlov avatar
    1167 posts

    Posted 12 Apr 2013 Link to this post

    Hello Robert,

    We did some refactoring to the RadTreeView control since the time of the post (02.17.2009). However, you are allowed to access the selected RadTreeViewItem by using the e.OriginalSourceproperty. Please try it out and led us know if you have any other questions.

    Kind regards,
    Pavel R. Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
DevCraft banner