Reset scrollbar

4 posts, 1 answers
  1. Naseem
    Naseem avatar
    70 posts
    Member since:
    Nov 2010

    Posted 29 Apr 2012 Link to this post

    Hi,

    I need to generate the RadMenuItems dynamically at run time, therefore when ever the contextmenu is opening I delete the previous items and re add new items.

    var sel = ((Telerik.Windows.Controls.RadMenuItem)BadRawContextMenu.Items.ToList();
    foreach (var item in sel)
    {
      selItem.Items.Remove(item);
    }

    Telerik.Windows.Controls.RadMenuItem newItem;
    foreach (var item in lstResult)
    {
        newItem = new Telerik.Windows.Controls.RadMenuItem() { Header = item.Title, IconTemplate = (System.Windows.DataTemplate)this.Resources["IconTemplateBestMatches"], Tag = item.Code };
        newItem.Click += new Telerik.Windows.RadRoutedEventHandler(newItem_Click);
        selItem.Items.Add(newItem);
    }

    The issue I'm dealing with is the scrool bar. if user change scroll bar position, next time when context menu is opened, it still shows the previous scroll bar position.
    Actually that make sense, because each time when the ContextMenu_Opened is fire , I just remove and re add items. I don't know how I can rest the scroll bar to move to the top of the list whenever the context menu opens?

    I'd be thankful if you could assist me on this,

    Best Regards,
    Naseem




  2. Ivo
    Admin
    Ivo avatar
    390 posts

    Posted 03 May 2012 Link to this post

    Hello,

    You can find the ScrollViewer into your RadContextMenu by ChildrenOfType<T> extension method and call the ScrollToVerticalOffset(0) method to it. Here is sample code:
    var contextMenu = (RadContextMenu)sender;
    var scrollViewer = contextMenu.ChildrenOfType<ScrollViewer>().FirstOrDefault();
    if (scrollViewer != null)
    {
        scrollViewer.ScrollToVerticalOffset(0);
    }

    This should do the trick.

    Regards,
    Ivo
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Naseem
    Naseem avatar
    70 posts
    Member since:
    Nov 2010

    Posted 03 May 2012 Link to this post

    Hi Ivo,

    Thank you so much for the solution . I've tried it but unfortunately it didn't work. The bellow line returns null !

     

     

    ContextMenu.ChildrenOfType<ScrollViewer>().FirstOrDefault();

    I thought maybe I need to add scrollview into ContextMenu and I did it as bellow

    <telerik:RadMenuItem Header="Best Matches" Style="{StaticResource RadMenuItemStyle1}">
                                <ScrollViewer VerticalScrollBarVisibility="Visible">
                                    <ItemsPresenter Margin="1"></ItemsPresenter>
                                </ScrollViewer>
                                <telerik:RadMenuItem.IconTemplate>
                                    <DataTemplate>
                                        <Image Source="/Aztec.Backstage.App.Fusion.Pres;Component/Images/001_15.png" Width="16" Height="16"/>
                                    </DataTemplate>
                                </telerik:RadMenuItem.IconTemplate>
    </telerik:RadMenuItem>


    I've attached two images which can show my issue. The (1.png) is for the first time Contextmenu is opened. If user changes the scroll bar position, the next time context menu opens , it still shows the last scrollbar position (2.jpg)

    I'd be thankful if you can asist me on this,

    Best Regards,
    Naseem

  5. Answer
    Ivo
    Admin
    Ivo avatar
    390 posts

    Posted 04 May 2012 Link to this post

    Hello,

    You can try this code on the ItemClick event of the RadContextMenu:
    var menuItem = e.Source as RadMenuItem;
    var scrollViewer = menuItem.ParentOfType<ScrollViewer>();
    scrollViewer.ScrollToVerticalOffset(0);

    Regards, Ivo
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top