RadMenu.ItemContainerStyle set in code fails

4 posts, 0 answers
  1. JC
    JC avatar
    3 posts
    Member since:
    May 2015

    Posted 15 May 2015 Link to this post

     I am following the documentation guide Binding to Dynamic Data with one change, the RadMenu.ItemContainerStyle is set from code and not from XAML.

    _menu = new RadMenu();
    var s = Application.Current.Resources["MenuItemStyle"] as Style;
    if (s != null)
    {
        _menu.ItemContainerStyle = s;
    }

    The XAML template matches the example, expect for the name of the nested items to match the class used to construct the menu items:

                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                        xmlns:local="clr-namespace:Anvil.Desktop.Windows8">
         
        <Style x:Key="MenuItemStyle" TargetType="telerik:RadMenuItem">
            <Setter Property="Header" Value="{Binding Text}"/>
            <Setter Property="ItemsSource" Value="{Binding Items}"/>
            <Setter Property="Icon" Value="{Binding IconUrl}" />
            <Setter Property="IconTemplate" >
                <Setter.Value>
                    <DataTemplate>
                        <Image Source="{Binding}" Stretch="None"/>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
         
    </ResourceDictionary>

    The resource dictionary is merged and available in the merged resources but the menu items are not rendered as in the with_itemcontainerstyle_set.png attachment and when removing the style setting code:

    _menu = new RadMenu();
    var s = Application.Current.Resources["MenuItemStyle"] as Style;
    if (s != null)
    {
        // _menu.ItemContainerStyle = s;
    }

    the class names of the menu items are displayed as in  without_itemcontainerstyle_set.png

    Any suggestions?
     

     

     

     

  2. JC
    JC avatar
    3 posts
    Member since:
    May 2015

    Posted 15 May 2015 Link to this post

    Missing screen shots - first post returned an error message
  3. UI for WPF is Visual Studio 2017 Ready
  4. JC
    JC avatar
    3 posts
    Member since:
    May 2015

    Posted 15 May 2015 Link to this post

    Solved + more details:

    1. I am using implicit themes and the noxml assemblies

    2. The Application merged dictionaries are cleared and loaded from one of the resource assemblies in UI for WPF Q1 2015

    3. XAML is NOT used in the application for window definitions, everything is done with code - except where DataTemplates are useful - as in this case for styling as a resource

     

    Solution:

    The resource used to replace the MenuItemStyle in MUST reference a base - BasedOn="{StaticResource RadMenuItemStyle}" -  to enable it to render as it completely replaces the current resource in the merged dictionary in Application.Current.Resources.

     

    <?xml version="1.0" encoding="utf-8" ?>
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                        xmlns:local="clr-namespace:Anvil.Desktop.Windows8">
     
        <Style x:Key="MenuItemStyle" TargetType="telerik:RadMenuItem" BasedOn="{StaticResource RadMenuItemStyle}">
            <Setter Property="Header" Value="{Binding Text}"/>
            <Setter Property="ItemsSource" Value="{Binding Items}"/>
            <Setter Property="Icon" Value="{Binding IconUrl}" />
            <Setter Property="IconTemplate" >
                <Setter.Value>
                    <DataTemplate>
                        <Image Source="{Binding}" Stretch="None"/>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
     
    </ResourceDictionary>
     

     

  5. Kalin
    Admin
    Kalin avatar
    1207 posts

    Posted 18 May 2015 Link to this post

    Hi,

    We are glad you have managed to resolve the issue. Indeed when using Implicit Styling the applied Styles should be always based on the default Styles of the control in order to override only the needed properties.

    If you have any other questions or concerns, let us know.

    Regards,
    Kalin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top