如何为模板元素创建事件?
我有机会在 WPF 中为模板中的控件执行事件而不创建 UserControl 等吗? 例如:创建的窗口模板有自定义的“关闭(X)”按钮。每个窗口都有相同的操作。有机会让它发挥作用吗?给出 Click 事件会关闭窗口吗?
我的意思是这样使用它:
<Window style="{StaticResource MyWindowTemplate}">...</Window>
并且不创建 Window 的自定义类,因为我希望有机会将它用于 Windows 的每个类。
那么有机会这样做吗?或者有类似或更好的解决方案吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为
Template
可以实现某种行为。它们是为了外观和感觉,而不是行为。对于行为,我们附加了属性和行为,当附加到其有效的目标依赖对象时,它们的行为完全相同。例如,在您的情况下,右上角的关闭按钮是一个困难的按钮,但窗口上的任何按钮都会关闭目标 UI,即在指定某些附加行为时关闭窗口本身。
因此,在上面的示例中,任何配置有附加行为的按钮
local:CloseBehavior.IsCloseButton="True"
都会使该按钮单击以关闭其祖先窗口。编辑:
CloseBehavior.IsCloseButton
类似于下面给出的内容。因此,在下面的代码中,当我们针对任何窗口上的任何按钮将IsCloseButton
附加属性设置为true
时,使用视觉和逻辑遍历,附加行为会搜索祖先窗口,然后将其关闭当点击时。我希望这有帮助。
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.
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 setIsCloseButton
attached property astrue
against 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.