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

RadMenu.ItemContainerStyle set in code fails

3 Answers 165 Views
Menu
This is a migrated thread and some comments may be shown as answers.
JC
Top achievements
Rank 1
JC asked on 15 May 2015, 05:16 AM

 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?
 

 

 

 

3 Answers, 1 is accepted

Sort by
0
JC
Top achievements
Rank 1
answered on 15 May 2015, 05:20 AM
Missing screen shots - first post returned an error message
0
JC
Top achievements
Rank 1
answered on 16 May 2015, 03:02 AM

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>
 

 

0
Kalin
Telerik team
answered on 18 May 2015, 08:08 AM
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
Tags
Menu
Asked by
JC
Top achievements
Rank 1
Answers by
JC
Top achievements
Rank 1
Kalin
Telerik team
Share this question
or