We have a RadContextMenu named “ContextMenu” and “miOpen” is RadMenuItem of the context menu, its created in XAML as shown below.
XAML CODE is: -
<
telerikNavigation:RadContextMenu.ContextMenu
>
<
telerikNavigation:RadContextMenu
x:Name
=
"ContextMenu"
IsOpen
=
"False"
Visibility
=
"Collapsed"
>
<
telerikNavigation:RadMenuItem
Header
=
"OpenForm"
Name
=
"miOpen"
Click
=
"miOpen_Click"
>
<
telerikNavigation:RadMenuItem.Icon
>
<
Image
Source
=
"../Images/Open.png"
Stretch
=
"None"
/>
</
telerikNavigation:RadMenuItem.Icon
>
</
telerikNavigation:RadMenuItem
>
</
telerikNavigation:RadContextMenu
>
</
telerikNavigation:RadContextMenu.ContextMenu
>
We want to Create SubMenus in “miOpen” at run time dynamically, these submenus items can change at every right click, here is the cs code for dynamic menu creation.
We are using RemoveAt() method as we have to change the SubMenus at every right click (but not in above cs code).
On First Right Click...above code working good but its giving error “Value does not fall within the expected range” after first rigth click.
Here is CS CODE: -
try
{
Telerik.Windows.Controls.RadMenuItem objRadMenuMainMenu =
(Telerik.Windows.Controls.RadMenuItem)
this
.FindName(
"miOpen"
);
for
(
int
k = 0; k<objRadMenuMainMenu.Items.Count;k++)
{
objRadMenuMainMenu.Items.RemoveAt(k);
}
for
(
int
i = 0; i < 5; i++)
{
Telerik.Windows.Controls.RadMenuItem objConnPart =
new
Telerik.Windows.Controls.RadMenuItem();
objConnPart.Header = i.ToString() +
"Header"
;
objConnPart.Name = i.ToString() +
"Name"
;
objConnPart.Tag = i.ToString() +
"Name"
;
objRadMenuMainMenu.Items.Insert(i, objConnPart);
}
ContextMenu.Visibility = Visibility.Visible;
}
catch
(Exception ex)
{
}
Here are the exception: -
Base exception:
Value does not fall within the expected range. at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, Boolean b) at System.Windows.DependencyObject.SetValue(DependencyProperty property, Boolean b) at System.Windows.Controls.Primitives.Popup.set_IsOpen(Boolean value) at Telerik.Windows.Controls.PopupManager.Open(Popup popup, Boolean attachToVisualRoot) at Telerik.Windows.Controls.PopupManager.OpenPopup(Popup popup, Boolean attachToVisualRoot) at Telerik.Windows.Controls.PopupManager.Open(Popup popup, PopupType type, Boolean attachToVisualRoot) at Telerik.Windows.Controls.PopupWrapper.ShowPopup() at Telerik.Windows.Controls.RadContextMenu.OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue) at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation) at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet) at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at Telerik.Windows.Controls.RadContextMenu.set_IsOpen(Boolean value) at Telerik.Windows.Controls.RadContextMenu.TriggerEventFired() at Telerik.Windows.Controls.RadContextMenu.OnEventTriggered(Object sender, EventArgs e) at Telerik.Windows.Controls.RadContextMenu.OnElementWithContextMenuMouseClick(Object sender, MouseButtonEventArgs e) at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
Please check above code and give us solution.
Ganesh,
MCL Systems.