如何从Silverlight中的XAML中定义的usercontrol正确继承 [英] How to correctly inherit from a usercontrol defined in XAML in Silverlight

查看:148
本文介绍了如何从Silverlight中的XAML中定义的usercontrol正确继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个我编写过的usercontrol(在Silverlight中),它使用XAML来定义它的外观,我该如何制作它的自定义版本?

If I have a usercontrol (in Silverlight) that I've written, that uses XAML to define it's appearance, how can I make a customised version of it?

即我有MyControl.xaml& MyControl.xaml.cs

i.e. I have MyControl.xaml & MyControl.xaml.cs

如果我想要一个SpecialisedControl子类,我该怎么办?我假设我只是创建一个新的代码文件,然后从MyControl继承。但是,如果我想改变基类的外观 - 那我该怎么办?

What do I need to do if I want a "SpecialisedControl" child class? I assume I just make a new code file, then inherit from MyControl. But what if I want to change the appearance of the base class - then what do I do?

推荐答案

我写这个想法你正在谈论WPF,而不是Silverlight,但可能有足够的重叠,这有用,所以我发布它,无论如何。

I wrote this thinking you were talking about WPF, rather than Silverlight, but there may be enough overlap for this to be helpful, so I'm posting it, anyway.

如果通过改变基类的外观你的意思是提供一个新的模板,那么你需要的可能是一个CustomControl,而不是一个UserControl。

If by "change the appearance of the base class" you mean "provide a new template", then what you need is probably a CustomControl, not a UserControl.

实现这一目标的最好方法是按照其他Microsoft控件设置的示例,如Button或ListBox:

The best way to accomplish this is to follow the example set by other Microsoft controls, such as Button or ListBox:


  1. 创建一个直接从Control派生的类(或者其他什么是最接近你的控件)。

  2. 如果需要向控件公开任何属性(例如按钮上的文本),请确保将它们正确地定义为DependencyProperties。

  3. 如上所述这里,创建一个名为Themes / generic.xaml的ResourceDictionary,并为你的类添加一个包含模板的样式(不要给样式一个键)。

  4. 对于需要从控件中获取值的控件上元素的任何属性,请使用TemplateBinding。

  5. 如果您需要将任何事件处理程序附加到模板中的元素,请为它们提供一个独特的名字。微软使用这些名称前缀为PART_的约定,我认为为了保持一致性这是一件好事,但并不是严格要求。

  6. 如果你需要的话附加事件处理程序,重载OnApplyTemplate()。在这个方法中,你应该分离任何旧的事件处理程序(我们当然不希望任何内存泄漏!),并查找具有您在模板中提供的名称的元素 - 当您找到它们时,根据需要附加事件处理程序。

  1. Create a class that derives directly from Control (or whatever is closest to your control).
  2. If any properties will need to be exposed to the control (such as text on a button, for example), make sure that you properly define them as DependencyProperties.
  3. As described here, create a ResourceDictionary called Themes/generic.xaml and add a style for your class that includes a template (don't give the style a key).
  4. Use TemplateBindings for any properties of elements on your control that need to get values from your control.
  5. If you'll need to attach any event handlers to elements in your template, give them a unique name. Microsoft uses the convention of prefixing these names with "PART_", and I think it's a good thing to do for the sake of consistency, but it's not strictly required.
  6. Again, if you need to attach event handlers, overload OnApplyTemplate(). In this method, you should detach any old event handlers (we certainly don't want any memory leaks!), and look for elements that have the names your provided in your template--when you find them, attach event handlers, as necessary.

这肯定比简单地从UserControl派生更多的工作,但如果你想能够完全重新模板控制,就像你可以使用内置控件一样,这是实现它的方法。

This is certainly much more work than simply deriving from UserControl, but if you want to be able to totally re-template controls, like you can with the built-in controls, this is the way to do it.

另一方面,如果你想要的话do是提供一定数量的有限自定义,例如更改背景,或将Command与某些用户操作相关联,那么最好的办法是公开DependencyProperties,然后可以在控件的样式中设置,或者控件的实例本身。

On the other hand, if all you want to do is to provide a certain amount of limited customization, such as changing the background, or associating a Command with some user action, then the best thing to do is to expose DependencyProperties, which can then be set in styles for your control, or on instances of your control, itself.

如果您提到要在继承控件中自定义外观,则该过程非常相似:只需添加默认样式即可新的控制使用新模板;如果你需要添加更多的事件处理程序,只需要确定你调用base.OnApplyTemplate()。

In the case you mentioned of wanting to customize the look in an inherited control, the process is pretty similar: just add a default style for the new control with a new template; if you need to add more event handlers, just be absolutely certain that you call base.OnApplyTemplate().

这篇关于如何从Silverlight中的XAML中定义的usercontrol正确继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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