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

LoadOnDemand called twice

1 Answer 110 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mike
Top achievements
Rank 1
Mike asked on 14 Sep 2010, 11:05 PM
Hi.

Using RadControls_for_Silverlight_4_2010_2_0812, I discovered that if I dynamically set
item.IsLoadOnDemandEnabled = true;
item.LoadOnDemand += SampleTreeView_LoadOnDemand;

Then LoadOnDemand will get called twice.

Take the example here:
http://www.telerik.com/community/forums/silverlight/treeview/treeview-with-loadondemand-and-checkboxes-does-not-remember-checkbox-state.aspx

And change:
1. MainPage.xaml
  a. remove IsLoadOnDemandEnabled="True"
  b. remove LoadOnDemand="SampleTreeView_LoadOnDemand"
  c. add ItemPrepared="SampleTreeView_ItemPrepared"
2. MainPage.xaml.cs
private void SampleTreeView_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)
{
RadTreeViewItem item = e.PreparedItem;

item.IsLoadOnDemandEnabled = true;
item.LoadOnDemand += SampleTreeView_LoadOnDemand;
}

Set a breakpoint in SampleTreeView_LoadOnDemand and you will see it get called twice when you expand "Team 1 \ John from Team 1" for example.

It will not get called twice if you just expand "Team 1".

Is this a bug?

Thank you.

1 Answer, 1 is accepted

Sort by
0
Accepted
Tina Stancheva
Telerik team
answered on 20 Sep 2010, 12:31 PM
Hello Mike,

The described behavior is expected since the LoadOnDemand() event is a routed event. Since you attach it to every RadTreeViewItem, it is first handled by its source ("Team 1 \ John from Team 1" for example) and then the event handlers of the parent elements are invoked (in this case the "Team 1" LoadOnDemand() event handler) till the event reaches the root element of the visual tree.

This is why when you set a breakpoint in the LoadOnDemand() event handler, it is hit twice. However, if you check the sender, you will notice that the objects raising the event are different. So in your scenario, you can handle the LoadOnDemand() event in order to stop it propagating further in the visual tree:
private void SampleTreeView_LoadOnDemand(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
    RadTreeViewItem item = e.OriginalSource as RadTreeViewItem;
    currentItem = item;
    e.Handled = true;
         
    System.Windows.Threading.DispatcherTimer pageTimer = new System.Windows.Threading.DispatcherTimer();
    pageTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // milliseconds
    pageTimer.Tick += new EventHandler(loadOnDemandTimer_Tick);
    pageTimer.Start();
}

I hope this information will help you. Let me know if I can further assist you.

Kind regards,
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
Tags
TreeView
Asked by
Mike
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Share this question
or