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

Reset scrollbar

3 Answers 25 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Naseem
Top achievements
Rank 1
Naseem asked on 30 Apr 2012, 04:40 AM
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




3 Answers, 1 is accepted

Sort by
0
Ivo
Telerik team
answered on 03 May 2012, 03:33 PM
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 >>

0
Naseem
Top achievements
Rank 1
answered on 04 May 2012, 01:53 AM
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

0
Accepted
Ivo
Telerik team
answered on 04 May 2012, 09:40 AM
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 >>

Tags
Menu
Asked by
Naseem
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Naseem
Top achievements
Rank 1
Share this question
or