何时使用表达式混合创建用户控件
因此,我正在开发一个新应用程序,并且我正在使用 Expression Blend(第一次)来创建布局和样式等。但我有一个关于何时要创建用户控件的问题。
我有一个蛀虫,我想用它作为很多东西的背景,但它实际上是边框中的边框 - 然后我们将把任何控件放入其中。我当然想重用它,最好的方法是什么。我想我想把它变成一个用户控件?
问题与标准边框不同 - 我不能只编辑资源字典中的样式,因为就像我说的 - 它是边框中的边框。
提前致谢。
So I am working on a new app and I am using Expression Blend (for the first time) to create the layout and styles etc. but I have a question about when I would want to create a user control.
I have a borer that I want to use as the background for lots of stuff, however it is really a border in a border - then we will drop whatever controls into it. I want to reuse this of course, what is the best way to do that. I am thinking I want to make it into a user control?
The problem is unlike a standard border - I can't just edit a style in a resource dictionary because like I said - it's a border in a border.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是一个允许 UIElement 内容的 UserControl 示例:
使用示例:
看起来像:
随意修改边框。
编辑:为了实际回答问题并在某种程度上重申我在对 Tim 的回答的评论中所说的话,UserControls 适合于此,因为它们直接封装视觉表示并允许组合,同时不需要太多自定义逻辑(从理论上讲,它们可能非常复杂,并且包含很多逻辑)。它们比 CustomControls 更轻量、更直接。
在 WPF 控件(即普通控件,包括从它们继承的 CustomControls)中,它们被认为是无外观的,它们通常提供默认模板,但它们的关键方面是它们的功能,如果您想到一个
ComboBox
例如,它是一个包含项目的控件,它应该有一个或没有选定的元素,并且应该有一个显示项目并支持选择等的下拉列表。控件的责任是提供构成此项目的必要属性和方法功能。由于有时需要某些视觉元素存在,控件可以定义 parts< /a> 代表前端的最小接口。查看控件创作概述,它更详细 -深度,可能比我更好地解释了很多事情。
另外:最终的轻量级方法是仅使用模板:
这会产生与 UserControl 相同的结果。
Here is a UserControl example that allows for UIElement content:
Usage example:
Looks like:
Just modify the borders at will.
Edit: To actually answer the question and somewhat reiterate what i said in a comment to Tim's answer, UserControls are appropriate for this since they directly encapsulate the visual representation and allow for composition while not requiring much custom logic (in theory they can be very complex though and include a lot of logic). They are more lightweight and immediate than CustomControls.
In WPF controls (i.e. normal controls, including CustomControls which just inherit from them) are considered to be lookless, they normally provide a default template but their key aspect is their functionality, if you think of a
ComboBox
for example, it's a control which contains items, it should have one or no selected element and there should be a dropdown which displays the items and supports selection etc. It is the responsibility of the control to provide the necessary properties and methods which make up this functionality. Since this sometimes requires certain visual elements to exist controls can define parts which represent the minimal interface to the front-end.Have a look at the Control Authoring overview, it is more in-depth and probably explains a lot of things better than i can.
Also: The ultimate lightweight method is only using Templates:
This yields the same result as the UserControl.
您可能最好不要制作
UserControl
,而是制作一个派生自ContentControl
或类似内容的自定义控件 - 我这样说是因为您打算拥有它包含其他控件,并且使用自定义控件比UserControl
更容易做到这一点。You'd probably be better off not making a
UserControl
and instead making a Custom Control that derives fromContentControl
or something similar - I say this because you're planning on having it contain other controls and that's a lot easier to do with Custom Controls thanUserControl
s.