I was wondering if there is a way to define a unified location for the context menu position, like always appear at "0,0" rather than the parent object position which called it.
Any ideas appreciated!
9 Answers, 1 is accepted
Thank you for writing.
You can position RadContextMenu anywhere with the help of the Offset and RegionOfInterest properties. If RegionOfInterest is null, the position of the menu owner is used as a region of interest instead.
Please write again if you have other questions.
Victor
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
My end goal is pretty simple. No matter where or what I click, I want my context menu to be right in the middle. Thanks in advance!
Thanks for writing.
If your context menu has the same height for every item (for example if the context menu has the same menu items always) you can simply set the RegionOfInterest to some rectangle on top of the page so that when the menu opens, it positions itself below it (which should be the center of the screen). Different screen sizes should not matter because you can calculate your region of interest once when the app is launched based on the screen size (also on orientation change if your app supports it).
If your context menus can be different however, you will have to tweak the region of interest every time a menu opens in order to take into account the height of the menu that will open.
It is obvious that these suggestions are less than ideal for this particular scenario. We will consider implementing absolute positioning of the context menu in the next release. For example contextMenu.AbsolutePosition = Center, or contextMenu.AbsolutePosition = Top.
Please write again if you have other questions and thanks for the feedback.
Victor
the Telerik team
Is there a roundabout way to just always set the position of the context menu in the code behind?
Please have a look at this help article. More concretely the RegionOfInterest section. When RegionOfInterest is not set, it uses the layout slot of the element with which the menu is associated. By setting RegionOfInterest you are explicitly telling the menu to use a custom rectangle.
Regards,Victor
Telerik
I tried to add a rect to my Region of Interest but I get a XAML / Wrapped / Runtime error.
.....
<telerikPrimitives:RadContextMenu.RegionOfInterest>
<Rect X="0" Y="200" Width="480" Height="800"></Rect>
</telerikPrimitives:RadContextMenu.RegionOfInterest>
.....
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at Telerik.Windows.Controls.StackVirtualizationStrategy.MeasureContainer(RadVirtualizingDataControlItem container)
at Telerik.Windows.Controls.VirtualizationStrategy.GetContainerForItem(IDataSourceItem item, Int32 insertAt)
at Telerik.Windows.Controls.StackVirtualizationStrategy.GetContainerForItem(IDataSourceItem item, Int32 insertAt)
at Telerik.Windows.Controls.VirtualizationStrategy.GetContainerForItem(IDataSourceItem item, Boolean insertLast)
at Telerik.Windows.Controls.VirtualizationStrategy.ManageLowerViewport(Boolean recycle)
at Telerik.Windows.Controls.RadVirtualizingDataControl.ManageViewport()
at Telerik.Windows.Controls.RadVirtualizingDataControl.BalanceVisualSpace()
at Telerik.Windows.Controls.RadVirtualizingDataControl.CleanupAfterCollectionReset()
at Telerik.Windows.Controls.RadVirtualizingDataControl.OnCollectionReset()
at Telerik.Windows.Controls.RadVirtualizingDataControl.UpdateViewportOnItemsChange(NotifyCollectionChangedEventArgs e)
at Telerik.Windows.Controls.RadVirtualizingDataControl.OnItemsChanged(NotifyCollectionChangedEventArgs e)
at Telerik.Windows.Controls.RadDataBoundListBox.OnItemsChanged(NotifyCollectionChangedEventArgs e)
at Telerik.Windows.Controls.RadJumpList.OnItemsChanged(NotifyCollectionChangedEventArgs e)
at Telerik.Windows.Controls.Primitives.DataControlBase.OnListSourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
at Telerik.Windows.Data.RadListSource.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at Telerik.Windows.Data.RadListSource.RefreshOverride()
at Telerik.Windows.Data.RadCollectionViewSource.RefreshOverride()
at Telerik.Windows.Data.RadListSource.Refresh()
at Telerik.Windows.Data.RadListSource.SourceCollectionChangedOverride(NotifyCollectionChangedEventArgs e)
at Telerik.Windows.Data.RadCollectionViewSource.SourceCollectionChangedOverride(NotifyCollectionChangedEventArgs e)
at Telerik.Windows.Data.RadListSource.OnSourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
at Telerik.Windows.Data.RadListSource.Telerik.Windows.Data.IWeakEventListener.ReceiveEvent(Object sender, EventArgs e)
at Telerik.Windows.Data.WeakEventHandler`1.ProcessEvent(Object sender, EventArgs e)
at Telerik.Windows.Data.WeakEventHandler`1.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at ShowSeek.RangeObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at ShowSeek.RangeObservableCollection`1.AddRange(IEnumerable`1 items)
at ShowSeek.MainPage.BuildMainList()
at ShowSeek.MainPage.<DataBinderWork>b__6()
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
Thanks for writing.
It appears that the XAML infrastructure does not like struct types, which Rect happens to be. You can try creating a sort of global view model with a property of type Rect that you can bind to. For example if your global view model is inserted in your app resources and its key is "ViewModel", you can try setting the region of interest like this:
<
telerikPrimitives:RadContextMenu
RegionOfInterest
=
"{Binding RegionOfInterest, Source={StaticResource ViewModel}}"
/>
Victor
Telerik