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

Add button click event to Custom RadShape

1 Answer 229 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Abhinav
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">
            <Setter.Value>
                <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}
                                                                                            ,Path=ButtonVisiblity,Mode=TwoWay}" 
                                  Click="Button_Click" Margin="5" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>    How can i do that???



1 Answer, 1 is accepted

Sort by
0
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;
    base.OnApplyTemplate();
}

I hope this helps.

Regards,
Martin
Telerik
 
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.
 
Tags
Diagram
Asked by
Abhinav
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or