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

how to set style of RadMenuItem in code-behind

2 Answers 229 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Bin
Top achievements
Rank 1
Bin asked on 26 Mar 2012, 08:22 PM
In xaml, I created a RadMenu and added a couple of RadMenuItems. I also created a style for RadMenuItem and applied the style to each of the RadmenuItems added in the xaml.

xaml:
<UserControl.Resources>
...
<Style x:Key="RadMenuItemStyle1" TargetType="telerik:RadMenuItem">  

...

</Style>

 

</UserControl.Resources> 

<telerik:RadMenu>

    <telerik:RadMenuItem Header="Item 1" Name="Item1">  

        <telerik:RadMenuItem Header="Sub Item 1" Style="{StaticResource RadMenuItemStyle1}" />

        <telerik:RadMenuItem Header="Sub Item 2" Style="{StaticResource RadMenuItemStyle1}" />

    </telerik:RadMenuItem>

</telerik:RadMenu>
...


Other than the RadMenuItems added in xaml, I have to add extra RadMenuItems at run-time. I can add the RadMenuItem at run-time, but I have difficulties applying the style created in xaml to the dynamically added RadMenuItem. Is it possible? If so, how?

code-behind:
RadMenuItem
newItem = new RadMenuItem();  

newItem.Header = "Newly added item";

//newItem.Style = how to reference the style created in xaml???
this
.Item1.Items.Add(newItem);

Thank you in advance!

2 Answers, 1 is accepted

Sort by
0
Bin
Top achievements
Rank 1
answered on 26 Mar 2012, 08:32 PM

Found the syntax:

 

newItem.Style = (

 

Style)(this.Resources["RadMenuItemStyle1"]);

 

0
Konstantina
Telerik team
answered on 30 Mar 2012, 07:20 AM
Hi,

This is the correct syntax. We are glad that you have resolved the issue yourself and shared it with the others.

Regards,
Konstantina
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Menu
Asked by
Bin
Top achievements
Rank 1
Answers by
Bin
Top achievements
Rank 1
Konstantina
Telerik team
Share this question
or