Get Selected Item Node of RadTreeMap in View Model and set un-select it

11 posts, 2 answers
  1. Ernie S
    Ernie S avatar
    33 posts
    Member since:
    Apr 2014

    Posted 21 Jul 2014 Link to this post

    How to I get the selectec TreeMapItem of a Rad Tree Map in code so I can set IsSelected to false?  Best I have been able to do is get the Selected Item that is backing the node but not the node itself.

    I have this:

    <telerik:RadTreeMap x:Name="TreeMap"
                        IsSelectionEnabled="True"
                        ItemsSource="{Binding ChartItems}"
                        LayoutStrategy="Squarified" >
     
        <telerikControl:EventToCommandBehavior.EventBindings>
            <telerikControl:EventBinding EventName="SelectionChanged"
                                         Command="{Binding ShowDataCommand}"
                                         PassEventArgsToCommand="True"/>
        </telerikControl:EventToCommandBehavior.EventBindings>
         
        ..............
        ..............
     
    </telerik:RadTreeMap>

    The argument (SelectionChangedRoutedEventArgs) gets passed properly but the SelectedItem is the object from the ChartItems collection that has the backing data and not the "RadTreeMapItem" which, I presume, is what I need to unselect it.  In other works, this works:

    SelectedChartItem = args.SelectedItem as ChartItem;

    but this does NOT:

    var SelectedTreeMapItem = args.SelectedItem as RadTreeMapItem;

    I assume (hope) that the item is buried somewhere else in the argument object?

    Thanks
    Ernie
  2. Evgenia
    Admin
    Evgenia avatar
    1406 posts

    Posted 24 Jul 2014 Link to this post

    Hi Ernie,

    I'm not sure why do you need such complex logic for selection of RadTreeMap items. You can wire to PreviewSelectionChanged event and just cancel the event like this:

    private void RadTreeMap_PreviewSelectionChanged(object sender, SelectionChangedRoutedEventArgs e)
       {
           if (e.SelectedItem == null)
               e.Cancel = true;
       }

    I also suggest that you review this online demo with source code provided where more complex selection of treemap items is demonstrated.

    Let me know if you need any further assistance.

    Regards,
    Evgenia
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. DevCraft banner
  4. Ernie S
    Ernie S avatar
    33 posts
    Member since:
    Apr 2014

    Posted 24 Jul 2014 in reply to Evgenia Link to this post

    What I am trying to show a popup that will display the details behind the selected chart item.  When the user closes the popup I want to the item they clicked on to be unselected.  And I want to do this in the ViewModel.  I can do with with PieCharts no problem like this:

    var args = e as ChartSelectionChangedEventArgs;

    DataPoint datapoint = args.AddedPoints.FirstOrDefault();
    if (datapoint == null)
        return;
     
    SelectedChartItem = datapoint.DataItem as ChartItem;
    datapoint.IsSelected = false;

    In this case the command binding passes the arguments as ChartSelectionChangedEventArgs which gets me access to the Charting DataPoint and the ability to change IsSelected.

    I need to do the same thing with TreeMap but since, it seems, treemap does not have a custom Event Argument (seems the command is sent back with a SelectionChangedRoutedEventArgs type parameter) I do not see how to do the same thing.

    Ernie

  5. Evgenia
    Admin
    Evgenia avatar
    1406 posts

    Posted 29 Jul 2014 Link to this post

    Hello Augustine,

    This is to inform you that we will need some more time to answer you. I'll contact you again till the end of the day. Thanks for your patience.

    Regards,
    Evgenia
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  6. Answer
    Evgenia
    Admin
    Evgenia avatar
    1406 posts

    Posted 29 Jul 2014 Link to this post

    You might find attached a sample that uses CommandBindings and some selection related boolean ViewModel properties to represent the scenario you have. Please review it and tell us whether such approach is helpful. Also something to mention here - as you will see the TreeMapItem might be unselected not only on clicking the Popup but on deselecting the same item. I believe this is more intuitive and this is what most customers will expect.

    Regards,
    Evgenia
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  7. Ernie S
    Ernie S avatar
    33 posts
    Member since:
    Apr 2014

    Posted 30 Jul 2014 in reply to Evgenia Link to this post

    Thank you Evgenia.  I cannot get the project to compile - it complains about a missing resource dictionary. But I can see what it is trying to do - you can to use the code behind of the View rather then being able to do it in the View Model which was hoping to avoid but it seems to be the only way to go.

    Thanks again,
    Ernie
  8. Ernie S
    Ernie S avatar
    33 posts
    Member since:
    Apr 2014

    Posted 06 Aug 2014 in reply to Evgenia Link to this post

    Hi Evgenia.  I was able to implement the code you used in my code (still cannot get your example to compile).

    I noticed that the deselection works but it seems the RadTreeMap.SelectedItem does not get updated.  If I click on an item for the first time and the .SelectedItem is non-null and running the for loop clears the IsSelected item so when the popup is closed the clicked item appears unselected.

    But if I click on the SAME item again, the .SelectedItem of the tree map is NULL.  If if I click on a different item it works just fine.  Seems the IsSelected change does not fully notify the TreeMap object that it is no longer selected.  SelectedItem is read-only so is there a way to clear the backing object?

    Thanks
    Ernie
  9. Answer
    Evgenia
    Admin
    Evgenia avatar
    1406 posts

    Posted 11 Aug 2014 Link to this post

    Hello Augustine,

    Please mind that I added RadTreeMapItem Style in the project previously sent and this style is BasedOn existing RadTreeMapItemStyle and has no Key set. It gets applied without calling it anywhere. We do this when we want the styles to get applied implicitly. You can read more about this approach in our documentation, more specifically here. Here's why I'm mentioning this - I guess that you have missed to add reference to the theme assembly that is used as source for the implicit style files merged in App.xaml. This explains why you are observing a missing resource dictionary exception and the project does not compile. Could you please check whether this is the case on your side?
    As for your last post - could you please send us a detailed steps that we should follow to reproduce your case? I'm not able to observe this by debugging the project I sent you.

    Regards,
    Evgenia
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  10. Ernie S
    Ernie S avatar
    33 posts
    Member since:
    Apr 2014

    Posted 11 Aug 2014 in reply to Evgenia Link to this post

    Thanks Evgenia.  I understand the implicit styles thing - I just do not seem to have the VisualStudio2013 theme you are using.  I just removed all references to it and the project now runs.

    I am not sure what the problem is with my current code.  I am use the Telerik EventBinding object to call a command with the PassEventArgsToCommand set to true.  When I do this for some reason the second pass comes across as null.

    I ended up just sending the entire treemap object back to the VM and used your children selection technique and that seems to work fine.

    Ernie
  11. Evgenia
    Admin
    Evgenia avatar
    1406 posts

    Posted 14 Aug 2014 Link to this post

    Hello Augustine,

    The onliest reason I can find for the strange behavior that you observe is if you have specified a CommandParameter to your EventBinding. In this case the PassEventArgsToCommand value is ignored.
    However I'm glad you managed to make this work. Feel free to drop us a line if there is anything else we can assist you with.

    Regards,
    Evgenia
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  12. Ernie S
    Ernie S avatar
    33 posts
    Member since:
    Apr 2014

    Posted 14 Aug 2014 in reply to Evgenia Link to this post

    Actually, if you look at my first post you will see exactly what I am doing if you wish to investigate it any further.

    Thanks again for the help.

    Ernie
Back to Top
DevCraft banner