Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7011753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:10:02+00:00 2026-05-27T22:10:02+00:00

I have create a lookless control to be used in a Silverlight 4 project.

  • 0

I have create a lookless control to be used in a Silverlight 4 project. This control contains a button and I would like to capture the click event. The Generic.xaml contains

<Style TargetType="TU:MyControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TU:MyControl" >
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}" d:DesignWidth="550" d:DesignHeight="228">
                    <Grid Background="Silver">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="150*"/>
                            <ColumnDefinition Width="60"/>
                            <ColumnDefinition Width="150*"/>
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="0" Margin="2" BorderBrush="DarkGray" BorderThickness="3"></Border>
                        <Border Grid.Column="2" Margin="2" BorderBrush="DarkGray" BorderThickness="3"></Border>
                        <StackPanel Grid.Column="1">
                            <Button Name="PART_MyClick"  Height="32" Width="32" Margin="0,8,0,0"></Button>
                        </StackPanel>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

After researching the problem I beleive that I have to add the following attribute to my control class

[TemplatePart(Name = "PART_MyClick", Type = typeof(Button))]

Then in my controls constructor I have added the following code

var myClick = GetTemplateChild("PART_MyClick") as Button;
if(myClick != null)
{
    myClick.Click += (o, e) => DoThings();
}

when run though the myClick variable is always null so the event handler never gets attached. Could you please tell me where I am going wrong? Im a newbie so if this is the wrong approach completly then any advise on the correct approach would also be greatfully received

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:10:03+00:00Added an answer on May 27, 2026 at 10:10 pm

    Override the OnApplyTemplate method and put your code there instead of the control’s constructor:

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        var myClick = GetTemplateChild("PART_MyClick") as Button;
        if(myClick != null)
        {
            myClick.Click += (o, e) => DoThings();
        }
    }
    

    Because during the constructor call the visual tree for the control is not build up yet. From MSDN OnApplyTemplate:

    Attach class-defined event handlers to parts of the template. For
    example, you might want class logic to handle KeyDown events from a
    TextBox template part so that UI states are updated, and other events
    that are specific to your control are raised instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a lookless control using silverlight 4. This control contains a textbox
I have create two tables with reference with another table: I like this: Table1:
i have create this code for my button but the image doesen't change why?
I have create name range on sheet A so I need to use this
This is what I currently have: CREATE OR REPLACE TRIGGER MYTRIGGER AFTER INSERT ON
I've read this book section about git branches. I have create a branch called
i have to create an asp.net page dynamically on runtime. It should work like
I have Create Many tables For User Registration Like Naukri... When i finished registration
I have create a LINQ-to-SQL project in Visual Studio 2010 using Dynamic Data. In
I have create the class HoverButton which derives from Form.Button. Here I override the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.