如何将方法附加到ResourceDictionary的ControlTemplate中定义的Button的Click属性? [英] How to attach a method to Click property of a Button defined in ControlTemplate in ResourceDictionary?

查看:129
本文介绍了如何将方法附加到ResourceDictionary的ControlTemplate中定义的Button的Click属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

在我的项目中,我有ResourceDictionary,其中定义了ControlTemplate以自定义ListBoxItem.该自定义的ListBox是UserControl的一部分,它在其自己的单独文件中定义.当用户单击某些ListBoxItem的按钮时,应调用相同ListBoxItem的方法.

有谁知道我该怎么做?我知道如何将ResourceDictionary后面的代码中定义的方法附加到Button的Click事件中,该ControlTemplate定义在其中.但是我不想那样做.我希望将所有UserControl的逻辑都定义在一个地方.任何帮助表示赞赏.

在此先谢谢您.

Dear all,

In my project I have ResourceDictionary in which I defined ControlTemplate to customize ListBoxItem. That customized ListBox is part of UserControl defined in its own separate file. When user clicks some ListBoxItem''s Button a method of the same ListBoxItem shoud be called.

Does anyone know how can I accomplish this? I know how to attach method defined in code behind ResourceDictionary which ControlTemplate is defined in to Button''s Click event. But I don''t want to do that. I want all my UserControl''s logic to be defined in one place. Any help is appreciated.

Thanks in advance.

推荐答案

在ControlTemplate中为按钮提供一个名称.您可以使用CS文件中的以下语句获取Button.
Provide a Name for the Button in the ControlTemplate. You can get the Button using the following statement in the CS file.
Button button = GetTemplateChild(YOUR_BUTTON_NAME_IN_THE_CONTROLTEMPLATE) as Button
button.Click += OnButtonClick;



注意:如果您派生ListBoxItem并在派生类中使用上面的代码,这将很好地工作.

更新:
错误删除.我不知道如何撤消删除操作. :(


如果有帮助,请将其标记为答案



Note: This would work fine if you derive the ListBoxItem and use the above code inside the derived class.

Update :
Deleted by mistake. I don''t know how to undo the delete action. :(


Mark it as answer if it is helpful


这篇关于如何将方法附加到ResourceDictionary的ControlTemplate中定义的Button的Click属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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