如何使用dependencyproperty语法附加WPF事件? [英] How do I attach a WPF event using dependencyproperty syntax?

查看:85
本文介绍了如何使用dependencyproperty语法附加WPF事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将事件附加到这样的WPF按钮:



I can attach an event to a WPF button like this:

<Button Click="Button_Click"/>





为什么我不能这样做:





So why can't I do this:

<Button>
    <Button.Click="Button_Click"/>
</Button>





???



当我以为我开始理解WPF时。 :(



我尝试了什么:



尝试搜索互联网但我得到了大量关于编写自己的DependencyProperty代码的教程。



???

Just when I thought I was beginning to understand WPF. :(

What I have tried:

Tried searching the internet but I get loads of tutorials on writing my own DependencyProperty code.

推荐答案

因为属性元素语法 [ ^ ]仅适用于属性,而不适用于事件。它主要用于无法将属性值表示为一个字符串,所以你不能使用属性语法 [ ^ ]。



事件处理程序只能指定为处理程序方法名称,因此不需要元素语法e对他们来说是有条件的。即使有,你提出的语法也不是有效的XML。



NB:可以绑定按钮的命令属性 [ ^ ]使用属性元素语法,虽然通常不需要。
Because the property element syntax[^] only applies to properties, not events. It's primarily intended for cases where you can't represent the property value as a string, so you can't use the attribute syntax[^].

Event handlers can only be specified as the handler method name, so there's no need for an element-syntax equivalent for them. Even if there was, the syntax you've proposed isn't valid XML.

NB: You can bind the button's Command property[^] using the property element syntax, although it's not normally required.


这篇关于如何使用dependencyproperty语法附加WPF事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆