Association of a menu Icon syntax

4 posts, 0 answers
  1. Corrado Cavalli
    Corrado Cavalli avatar
    6 posts
    Member since:
    Feb 2007

    Posted 17 Sep 2009 Link to this post

    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

     

  2. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 17 Sep 2009 Link to this post

    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.
  3. DevCraft banner
  4. Corrado Cavalli
    Corrado Cavalli avatar
    6 posts
    Member since:
    Feb 2007

    Posted 17 Sep 2009 Link to this post

    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?
  5. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 18 Sep 2009 Link to this post

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