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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:26:45+00:00 2026-05-26T08:26:45+00:00

I there any chance in WPF to do a event for control in template

  • 0

I there any chance in WPF to do a event for control in template not creating a UserControl or so on?
For example: created window template has custom “Close(X)” button. It has the same operation for every windows. It is any chance to make it working? Give Click event which will close the window?

I mean to use it like this:

<Window style="{StaticResource MyWindowTemplate}">...</Window>

And doesnt create custom class of Window because I want to have opportunity to use it to every classes of Windows.

So there is any chance to do it like this? Or any similar or better solution?

  • 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-26T08:26:46+00:00Added an answer on May 26, 2026 at 8:26 am

    I dont think Template can achieve a behavior. They are for look and feel but not behavior. For behaviors we have attached properties and behaviors which when attached to their valid target dependency objects to behave all the same.

    e.g. in your case the close button on the top right corner is a difficult one but any button on the window close a target UI i.e. Window itself when specified with some attached behavior.

     <Window x:Class="..."
             ...>
        <Grid>
            <Grid.RowDefinitions>
               <RowDefinition Height="*"/>
               <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ContentControl ... />
            <Buton Width="100"
                   Content="Close"
                   local:CloseBehavior.IsCloseButton="True" />
       </Grid>
     </Window>
    

    So in the example above any button that is configured with an attached behavior local:CloseBehavior.IsCloseButton="True" makes that button click to close its ancestor window.

    EDIT:

    CloseBehavior.IsCloseButton is something like given below. So in the code below when we set IsCloseButton attached property as true against any button on any window, using visual and logical traversal the attached behavior searches the ancestor window and then closes it when clicked.

    public static class CloseBehavior
    {
        public static readonly DependencyProperty IsCloseButtonProperty
            = DependencyProperty.RegisterAttached(
                "IsCloseButton",
                typeof (bool),
                typeof (CloseBehavior),
                new PropertyMetadata(
                   false,
                   OnIsCloseButtonPropertyChanged));
    
        private static void OnIsCloseButtonPropertyChanged
            (DependencyObject depObj,
             DependencyPropertyChangedEventArgs e)
        {
            var buttonBase = depObj as ButtonBase;
            if (buttonBase != null && (bool)e.NewValue)
            {
                buttonBase.Click
                    += (o, args) =>
                        {
                            var window
                                = GetVisualLogicalParent(
                                      buttonBase,
                                      typeof(Window)) as Window;
    
                            if (window != null)
                            {
                                window.Close();
                            }
                        };
            }
        }
    
        public static bool GetIsCloseButton(DependencyObject depObj)
        {
            return (bool)depObj.GetValue(IsCloseButtonProperty);
        }
    
        public static void SetIsCloseButton(
            DependencyObject depObj,
            bool value)
        {
            depObj.SetValue(IsCloseButtonProperty, value);
        }
    
        public static DependencyObject GetVisualLogicalParent(
           DependencyObject depObj,
           Type type)
        {
            if (depObj != null)
            {
                var parent = VisualTreeHelper.GetParent(depObj);
                if (parent == null)
                {
                    parent = LogicalTreeHelper.GetParent(depObj);
                }
                if (parent != null)
                {
                    if (type.IsInstanceOfType(parent))
                    {
                        return parent;
                    }
                    else
                    {
                        return GetVisualLogicalParent(parent, type);
                    }
                }
            }
    
            return null;
        }
    }
    

    I hope this helps.

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

Sidebar

Related Questions

Is there any way to tell how the ComboBox_SelectionChanged event was raised in WPF.
In WPF, I am creating a simple custom control for my TODO program. It
Is there any way to change the value of property at runtime in WPF
Is there any chance to get this work? I want my tests to be
Is there any chance that a SHA-1 hash can be purely numeric, or does
By any chance, is there any easy way to set a default MasterView for
Are there any limitations with respect to developing ASP.NET and WPF apps using VS
Curious if there are any disadvantages to using an ElementHost to host a WPF
I have a WPF UserControl, which I use in a WinForms Control within an
I have a Custom TabControl in a WPF application (attention: the technology is not

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.