RadControl Inheritance

4 posts, 0 answers
  1. Todor
    Todor avatar
    10 posts
    Member since:
    Aug 2008

    Posted 28 Aug 2008 Link to this post

    Can you give me an example walkthrough how to implement a custom Silverlight control, which inherits your RadMenu control for example ? So I can extend the functionallity the way I need it to.

    Thanks.

    Edit:
    Let me explain why I am asking this question and what problems I have.

    I have a CustomControl for example called MyControl. That is the .xaml file of the control:
    <UserControl x:Class="SilverlightCustomControls.MyControl"   
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"            
        Width="400" Height="300">    
    </UserControl> 

    This is what I have made in the .cs file of the control:
    using Telerik.Windows.Controls; 
     
    namespace SilverlightCustomControls 
        public partial class MyControl : RadMenu 
        { 
            public MyControl() 
            { 
                InitializeComponent(); 
            } 
        } 




    In my other CustomControl, which is a container, I want to use MyControl and I add it like this:
    <UserControl x:Class="SilverlightCustomControls.MyContainterClass" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:custom="clr-namespace:SilverlightCustomControls;assembly=SilverlightCustomControls"
        <Grid x:Name="LayoutRoot"
            <Grid.RowDefinitions> 
                <RowDefinition Height="26" /> 
                <RowDefinition Height="26" /> 
                <RowDefinition Height="*" /> 
            </Grid.RowDefinitions> 
             
            <custom:MyControl Name="MainMenu" Grid.Row="0"
                <custom:MyControl.Background> 
                    <LinearGradientBrush EndPoint="0.0,1.0" StartPoint="0.0,0.0"
                        <GradientStop Color="Gold" Offset="0.0"/> 
                        <GradientStop Color="White" Offset="0.2"/> 
                        <GradientStop Color="Gold" Offset="0.8"/> 
                        <GradientStop Color="Goldenrod" Offset="1"/> 
                    </LinearGradientBrush> 
                </custom:MyControl.Background> 
            </custom:MyControl>  
        </Grid> 
    </UserControl> 
     

    But when I try to run this application (the build is OK), I get this Exception:

    System.Windows.Markup.XamlParseException occurred 
      Message="Cannot specify the value multiple times for property: MyControl.Background. [Line: 20 Position: 45]" 
      LineNumber=20 
      LinePosition=45 
      StackTrace: 
           at System.Windows.Application.LoadComponent(Object component, Uri xamlUri) 
           at SilverlightCustomControls.MyContainterControl.InitializeComponent() 
           at SilverlightCustomControls.MyContainterControl..ctor() 
      InnerException:  
     

  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 29 Aug 2008 Link to this post

    Hello Todor,

    Thank you for using our controls.

    We don't have an example showing how to extend our RadControls. We are doing our best to provide controls that satisfy all developer's needs.
    Could you please elaborate a bit more on what you are trying to achieve so that we can give you a proper reply?

    Sincerely yours,
    Hristo
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Todor
    Todor avatar
    10 posts
    Member since:
    Aug 2008

    Posted 29 Aug 2008 Link to this post

    The problem is simple. I inherit a CustomControl from RadMenu and after that I add the Inherited control to another CustomControl as content. When I try to access the background property of the inherited control from the XAML file of the container control, I get the exception that I cannot set this property twice (as shown in my previous post). I think this problem occurs only when I use a LinearGradientBrush as a background (or any other brush, different from a simple color only). I am not sure if this is some kind of a bug inside the Telerik control, or I am just not familiar enough with the concepts of inheritance in Silverlight.
  4. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 01 Sep 2008 Link to this post

    Hello Todor,

    All RadControls for Silverlight 2 are custom controls, not user controls. You can extend them as you can extend every other control using inheritance. However, if you need to change the appearance of a control, you can do this without inheriting the control. You need to edit the default ControlTemplate and make the control look and feel like as per your custom requirements.

    As for the error - this is indeed a bug in the Silverlight framework that has been reported to Microsoft. I hope it will be fixed with the forthcoming RTM build.
    You can work around this problem by setting the Background property as an attribute like:
    <custom:MyControl Name="MainMenu" Grid.Row="0" Background="{StaticResource brush}"/> 

    Furthermore, you need to define your brush in the resources, like: 
    <UserControl.Resources> 
       <LinearGradientBrush x:Key="brush" EndPoint="0.0,1.0" StartPoint="0.0,0.0">  
          <GradientStop Color="Gold" Offset="0.0"/>  
          <GradientStop Color="White" Offset="0.2"/>  
          <GradientStop Color="Gold" Offset="0.8"/>  
          <GradientStop Color="Goldenrod" Offset="1"/>  
       </LinearGradientBrush> 
    </UserControl.Resources> 


    I had problems running the sample code you provided. I had to change it.
    Below is the modification I made in the MyControl.xaml:
    <custom:RadMenu x:Class="SilverlightCustomControls.MyControl"     
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:custom="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
        Width="400" Height="300">  
          
    </custom:RadMenu> 

    I was wondering why you inherited from RadMenu and made it a UserControl. Could you please elaborate a bit more on this?

    Regards,
    Hristo
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top