WPF - ControlTemplate 上的事件?
有谁知道为什么我无法在控件模板上设置事件?
例如,以下代码行将无法编译。它对控件模板中的任何事件执行此操作。
<ControlTemplate x:Key="DefaultTemplate" TargetType="ContentControl">
<StackPanel Loaded="StackPanel_Loaded">
</StackPanel>
</ControlTemplate>
我正在使用 MVVM 设计模式,此处的控件位于添加到应用程序的 MergedDictionaries 的 ResourceDictionary 中。
Does anyone know why I can't set an event on a control template??
For example, the following line of code will not compile. It does this with any events in a control template.
<ControlTemplate x:Key="DefaultTemplate" TargetType="ContentControl">
<StackPanel Loaded="StackPanel_Loaded">
</StackPanel>
</ControlTemplate>
I am using a MVVM design pattern and the control here is located in a ResourceDictionary that is added to the application's MergedDictionaries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,您可以...但是您希望在哪里定义事件处理程序?
ResourceDictionary
没有代码隐藏,因此没有地方放置事件处理程序代码。但是,您可以为资源字典创建一个类,并将其与x:Class
属性关联:C# 代码:(
您可能还需要将资源字典的构建操作更改为“Page “,具体记不太清了……)
Actually, you can... But where would you expect the event handler to be defined ? The
ResourceDictionary
has no code-behind, so there is no place to put the event handler code. You can, however, create a class for your resource dictionary, and associate it with thex:Class
attribute :C# code :
(you might also need to change the build action of the resource dictionary to "Page", I don't remember exactly...)