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

Association of a menu Icon syntax

3 Answers 80 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Corrado Cavalli
Top achievements
Rank 2
Corrado Cavalli asked on 17 Sep 2009, 12:50 PM
I'd like to know why following syntax works:

 

 

<telerikNavigation:RadMenuItem.Icon>

 

 

 

<Image Source="/MyApp;Component/Images/users.png"

 

 

Width="20"

 

 

Height="20" />

 

 

 

</telerikNavigation:RadMenuItem.Icon>

While this one fails:
1-Defined a resource named UsersImage

 

<

 

Image Source="/MyApp;Component/Images/users.png" Width="20" Height="20"

 

 

 

 

 

x:Name="UsersImage" />
2-Set Icon

 

<

 

telerikNavigation:RadMenuItem Header="Test"

 

 

IsEnabled="True" Icon={StaticResource UsersImage}>

Thanks
-Corrado

 

3 Answers, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 17 Sep 2009, 02:21 PM
Hello Corrado,

You should not set x:Name on elements inside RadMenu because this triggers a bug in the Silverlight Popup control. We reported this problem to Microsoft, but it wasn't fixed in Silverlight 2 and 3. I hope that they will fix it in Silverlight 4. For the time being, if you need to access the properties of the RadMenuItems or, as in your case, the Image in the Icon property, I would recommend databinding the control and keeping references to the view model. Most of our online examples are databound, you could check them for reference. If you have questions, please, do not hesitate to ask.

Sincerely yours,
Valeri Hristov
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
Corrado Cavalli
Top achievements
Rank 2
answered on 17 Sep 2009, 04:11 PM
There's no x:Name set on any MenuItem, I just need to set a static image to Icon property using
Icon={StaticResouce MyImageResource} why isn't that supported and i need to use expanded syntax?
0
Hristo
Telerik team
answered on 18 Sep 2009, 07:17 AM
Hi Corrado Cavalli,

This is not supported because when you define the Image as a resource it cannot be reused in more then one place. In other words you cannot have the same image instance shown twice. This is true for all UIElements. But there is an easy workaround. You can define your image uri as string in the resources and the bind the RadMenuItem.Icon property and with the use of converter you can return (create) new Image instances and set its source property to the value that come to the converter.

Here is a sample how you can do it.
<UserControl x:Class="SilverlightApplication1.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:SilverlightApplication1" 
    xmlns:mscorlib="clr-namespace:System;assembly=mscorlib" 
    xmlns:nav="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation">  
    <Grid x:Name="LayoutRoot">  
        <Grid> 
            <Grid.Resources> 
                <local:StringToImageConverter x:Key="converter" /> 
                <mscorlib:String x:Key="icon">Icons/insertHyperlink.png</mscorlib:String> 
            </Grid.Resources> 
            <nav:RadMenu VerticalAlignment="Top" > 
                <nav:RadMenuItem Header="Item 1" Icon="{Binding Source={StaticResource icon}, Converter={StaticResource converter}, ConverterParameter=None}">  
                    <nav:RadMenuItem Header="Item 1.1" Icon="{Binding Source={StaticResource icon}, Converter={StaticResource converter}, ConverterParameter=None}" /> 
                </nav:RadMenuItem> 
            </nav:RadMenu> 
        </Grid> 
    </Grid> 
</UserControl> 

and code behind:
using System;  
using System.Windows.Controls;  
using System.Windows.Media;  
using System.Windows.Media.Imaging;  
 
namespace SilverlightApplication1  
{  
    public partial class MainPage : UserControl  
    {  
        public MainPage()  
        {  
            InitializeComponent();  
        }  
    }  
 
    public class StringToImageConverter : System.Windows.Data.IValueConverter  
    {
        #region IValueConverter Members  
 
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
        {  
            Image image = new Image();  
            image.Stretch = (Stretch)Enum.Parse(typeof(Stretch), System.Convert.ToString(parameter), true);  
            Uri source = new Uri(System.Convert.ToString(value), UriKind.Relative);  
            BitmapImage bitmap = new BitmapImage();  
            bitmap.UriSource = source;  
            image.Source = bitmap;  
 
            return image;  
        }  
 
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
        {  
            throw new NotImplementedException();  
        }
        #endregion  
    }  

Let us know if this works for you.

Regards,
Hristo
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.
Tags
Menu
Asked by
Corrado Cavalli
Top achievements
Rank 2
Answers by
Valeri Hristov
Telerik team
Corrado Cavalli
Top achievements
Rank 2
Hristo
Telerik team
Share this question
or