Add button click event to Custom RadShape

2 posts, 0 answers
  1. Abhinav
    Abhinav avatar
    5 posts
    Member since:
    Apr 2014

    Posted 11 Jun 2014 Link to this post

    Hello there,

    I want to add button click event to a button present in the Resource Dictionary (Generic.xaml) of a custom radShape 

    <Style TargetType="{x:Type local:SideView}">
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Height" Value="50"/>
            <Setter Property="Width" Value="100"/>
            <Setter Property="Template">
                    <ControlTemplate TargetType="{x:Type local:SideView}">
                        <Grid Background="{TemplateBinding Background}">

                            <Rectangle Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}" 
                                       Height="{TemplateBinding Height}"
                                       Width="{TemplateBinding Width}"/>
                             <Button  HorizontalAlignment="Right" Height="20" Width="20" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}
                                      Click="Button_Click" Margin="5" />
        </Style>    How can i do that???

  2. Martin
    Martin avatar
    1042 posts

    Posted 16 Jun 2014 Link to this post

    Hi Abhinav,

    In order to add click event in the Generic.xaml of your custom control you can do the following:
    • Set x:Name on your Button 
    • Override the OnApplyTemplate() method of your control
    • Inside the method, get the Button with the GetTemplateChild() method
    • Add click event to the Button

    Here is an example:

    public override void OnApplyTemplate()
        Button btn = (Button)this.GetTemplateChild("myShapeBtn");
        btn.Click += btn_Click;

    I hope this helps.

    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
  3. DevCraft R3 2016 release webinar banner
Back to Top