LoadOnDemand called twice

2 posts, 1 answers
  1. Mike
    Mike avatar
    17 posts
    Member since:
    Jul 2010

    Posted 14 Sep 2010 Link to this post


    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:

    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.

  2. Answer
    Tina Stancheva
    Tina Stancheva avatar
    3298 posts

    Posted 20 Sep 2010 Link to this post

    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);

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