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 8175189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:41:23+00:00 2026-06-06T22:41:23+00:00

I want to create a new custom control derived from ContentControl (it will be

  • 0

I want to create a new custom control derived from ContentControl (it will be a container for other controls in a window), but I want a button to close it. (in fact I want a borderless window, but with system-like button to close it).

So I created the Style for the control which contains a Grid with two rows, in the upper row there’s a StackPanel with a single button.

How can I bind the Click event of the button to the control itself, to raise an event, or even to send Close command to the parent window?

<Grid>
<Grid.RowDefinitions>
    <RowDefinition Height="20" />
    <RowDefinition />
</Grid.RowDefinitions>
<Border Background="Azure" Grid.Row="0">
    <StackPanel Orientation="Horizontal">
        <Button HorizontalAlignment="Right" Content="X" Click="Close_Click" />
    </StackPanel>
    </Border>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1"/>
</Grid>

And the code behind:

static STContentControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(STContentControl), new FrameworkPropertyMetadata(typeof(STContentControl)));
}

public void Close_Click(object sender, RoutedEventArgs ea)
{

}
  • 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-06-06T22:41:26+00:00Added an answer on June 6, 2026 at 10:41 pm

    It sounds like you have created the template as a resource so it’s being applied to the control at runtime.

    You need to make sure you wire up your click event for the button in the OnApplyTemplate method (override this on your control).

    http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.onapplytemplate.aspx

    So you would override this on your UC like so:

    class NewUC : UserControl
    {
        public event EventHandler CloseClicked;
    
        public override void OnApplyTemplate()
        {
            Button btn = this.FindName("SomeButton") as Button;
    
            if (btn == null) throw new Exception("Couldn't find 'Button'");
    
            btn.Click += new System.Windows.RoutedEventHandler(btn_Click);
        }
    
        void btn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OnCloseClicked();
        }
    
        private void OnCloseClicked()
        {
            if (CloseClicked != null)
                CloseClicked(this, EventArgs.Empty);
        }
    }    
    

    I added a CloseClicked event to the example which you could handle in your parent window. This isn’t a routed event so you would have to manually wire it up in the parent control

    Methinks you could also use MouseLeftButtonDown routed event and check to see if the button was clicked at the parent level – will have a go myself…

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

Sidebar

Related Questions

I want to create a Custom Control derived from ComboBox , however if I
I want to create a custom control in C# which I will use multiple
I want to create custom templated control which controls inside of template coluld be
I want to create custom control (let's name it DesignPanel ) that will allow
I want to create a new web-browser for android, but this browser is not
I would like to create a custom WPF control that inherits from comboBox. So
I have a custom control for a LayoutPanel that holds child controls. I want
I have a class derived from slider which uses a custom control template and
I create a new asp.net custom control. This control get its data via a
I want to create custom WPF control that has a single child control inside.

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.