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

Dynamically Add RadMenuItem at code behind

7 Answers 331 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Cheng Lim Sek
Top achievements
Rank 1
Cheng Lim Sek asked on 03 Nov 2009, 11:24 AM
How can i dynamically add the radmenuitem at code behind.

I Have code like this
<Style>
....................................................................

<

 

Grid x:Name="MenuGrid" Height="25" Margin="0,0,0,0" Opacity="1" Background="Black">

 

<telerikNavigation:RadMenu Background="#304256" Name="RadMenu1">

 

 

 

<telerikNavigation:RadMenuItem Header="File" Background="#304256" Foreground="#C0DDF8">

 

 

 

<telerikNavigation:RadMenuItem Header="New Tab" Foreground="#A6C1D9" Background="#35465A">

 

 

 

</telerikNavigation:RadMenuItem>

 

 

 

</telerikNavigation:RadMenuItem>

 

 

 

<telerikNavigation:RadMenuItem Header="Edit" Background="#304256" Foreground="#C0DDF8">

 

 

 

<telerikNavigation:RadMenuItem Header="Copy" Foreground="#A6C1D9" Background="#35465A">

 

 

 

</telerikNavigation:RadMenuItem>

 

 

 

</telerikNavigation:RadMenuItem>

 

 

 

</telerikNavigation:RadMenu>

 

 

 

</Grid>
..................................................................
</Style>


Instead of hardcode like this, i want to create the RadMenu and RadMenuItem in Grid (MenuGrid) at code behind dynamically.
How can i do that? Example code will highly appreciate.

 

7 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 05 Nov 2009, 10:10 AM
Hello Cheng Lim Sek,

RadMenu and RadMenuItems are ItemsControl, that means they are exposing a property  -Items, where all the children are stored. To add a new item you just need to call radManu.Items.Add(new RadMenuItem()) method. Attached is a project that you requested.

Sincerely yours,
Kaloyan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Cheng Lim Sek
Top achievements
Rank 1
answered on 06 Nov 2009, 07:47 AM
hi, your answer have partially solve my problem.

Maybe i should let you know where i wan to load the radmenu.

<

 

UserControl x:Class="TelerikThemingApplication.RadWindowBlend"

 

 

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

 

 

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

 

xmlns:mscorlib="clr-namespace:System;assembly=mscorlib"

 

 

xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"

 

 

xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"

 

 

xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"

 

 

Width="400" Height="300">

 

 

 

<UserControl.Resources>

 

 

 

<Style x:Key="DropdownStyle" TargetType="Button">

 

 

 

<Setter Property="Template">

 

 

 

<Setter.Value>

 

 

 

<ControlTemplate TargetType="Button">

 

 

 

<Grid>

 

 

 

<VisualStateManager.VisualStateGroups>

 

 

 

<VisualStateGroup x:Name="FocusStates">

 

 

 

<VisualState x:Name="Focused"/>

 

 

 

<VisualState x:Name="Unfocused"/>

 

 

 

</VisualStateGroup>

 

 

 

<VisualStateGroup x:Name="CommonStates">

 

 

 

<VisualStateGroup.Transitions>

 

 

 

<VisualTransition GeneratedDuration="0:0:0.2" To="MouseOver"/>

 

 

 

<VisualTransition GeneratedDuration="0:0:0.1" To="Normal"/>

 

 

 

</VisualStateGroup.Transitions>

 

 

 

<VisualState x:Name="Normal"/>

 

 

 

<VisualState x:Name="MouseOver">

 

 

 

<Storyboard>

 

 

 

<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:02" Storyboard.TargetName="DropDown" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">

 

 

 

<EasingDoubleKeyFrame KeyTime="00:00:01" Value="23"/>

 

 

 

</DoubleAnimationUsingKeyFrames>

 

 

 

</Storyboard>

 

 

 

</VisualState>

 

 

 

<VisualState x:Name="Pressed"/>

 

 

 

<VisualState x:Name="Disabled"/>

 

 

 

</VisualStateGroup>

 

 

 

</VisualStateManager.VisualStateGroups>

 

 

 

<Grid x:Name="DropDown">

 

 

 

<Grid.RenderTransform>

 

 

 

<TransformGroup>

 

 

 

<ScaleTransform/>

 

 

 

<SkewTransform/>

 

 

 

<RotateTransform/>

 

 

 

<TranslateTransform/>

 

 

 

</TransformGroup>

 

 

 

</Grid.RenderTransform>
<telerikNavigation:RadMenu x:Name="radMenu1">
<
/telerikNavigation:RadMenu >
</Grid>

 

 

 

<Rectangle Fill="#C0DDF8" Stroke="White" StrokeThickness="0"/>

 

 

 

</Grid>

 

 

 

</ControlTemplate>

 

 

 

</Setter.Value>

 

 

 

</Setter>

 

 

 

</Style>

 

 

 

</UserControl.Resources>

 

 

 

<Grid x:Name="LayoutRoot" Background="White">

 

 

 

<Button Height="23" Style="{StaticResource DropdownStyle}" VerticalAlignment="Top"/>

 

 

 

</Grid>

 

</

 

UserControl>


I wanted to put the RadMenu where the radMenu1 is.
By using the code you provide, i can create it but it already cover my button.

 

0
Kaloyan
Telerik team
answered on 06 Nov 2009, 09:12 AM
Hi Cheng Lim Sek,

In case you want to achieve  a final result like menu placed in a button you can refer to our new controls in our Q3 official release - RadButton. Also some other easy approach is to use a toggle button and RadContextMenu. Check the Valeri's blog post describing this interesting approach. Let us know if this is not acceptable for you.

Sincerely yours,
Kaloyan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Cheng Lim Sek
Top achievements
Rank 1
answered on 06 Nov 2009, 09:20 AM
what i want to do is, i have a rectangle in front and another rectangle at the back. when i mouseover to the front rectangle, it will show me the back rectangle like a drop down. in the drop down rectangle, i put a grid and in the grid i put the radMenu. That is why i convert the rectangle to button to have the mouseover event to drop down the back rectangle.

is it do not have a way to insert the radMenu in the grid that i mention?
0
Kaloyan
Telerik team
answered on 12 Nov 2009, 11:40 AM
Hi Cheng Lim Sek,

Thank you for the clarification. Attached is a project that included the desired result. Let us know if this is acceptable for you.

Greetings,
Kaloyan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
jnh999
Top achievements
Rank 1
answered on 01 Dec 2010, 07:43 PM
The sample provided above crates a dynamic ContextMenu but it took up the LayoutRoot.  I need to create a RadContextMenu that pops up a small child window where mouse is clicked without hiding everything on the screen.  Is there a sample like that somewhere?
0
Kaloyan
Telerik team
answered on 06 Dec 2010, 10:30 AM
Hi jnh999,

The example actually represent the usate of the RadMenu. Find our online example with some related Contextmenu examples and further information. Let us know if any particular case appears.

Best wishes,
Kaloyan
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
Tags
Menu
Asked by
Cheng Lim Sek
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Cheng Lim Sek
Top achievements
Rank 1
jnh999
Top achievements
Rank 1
Share this question
or