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

Add button click event to Custom RadShape

1 Answer 215 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Abhinav asked on 11 Jun 2014, 12:09 PM
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???

1 Answer, 1 is accepted

Sort by
Martin Ivanov
Telerik team
answered on 16 Jun 2014, 10:25 AM
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.
Asked by
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question