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?
I dont think
Templatecan 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.
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.IsCloseButtonis something like given below. So in the code below when we setIsCloseButtonattached property astrueagainst any button on any window, using visual and logical traversal the attached behavior searches the ancestor window and then closes it when clicked.I hope this helps.