how to set style of RadMenuItem in code-behind

3 posts, 0 answers
  1. Bin
    Bin avatar
    9 posts
    Member since:
    Jun 2011

    Posted 26 Mar 2012 Link to this post

    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. Bin
    Bin avatar
    9 posts
    Member since:
    Jun 2011

    Posted 26 Mar 2012 Link to this post

    Found the syntax:

     

    newItem.Style = (

     

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

     

  3. DevCraft banner
  4. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 30 Mar 2012 Link to this post

    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 >>
Back to Top