为什么我需要 SilverLight 中的 ContentPresenter?
如果我可以用一个 TextBox
替换它(就像在按钮上一样), 或者我可以直接将媒体元素添加到Grid
(无论如何)...
ContentPresenter
的用途是什么?有一些优点吗?
If I can replace it with a single TextBox
(like on a button),
or I can add media element directly to Grid
(whatever)...
What is ContentPresenter
for? Is there some advantages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您并不总是需要 ContentPresenter。它充当占位符,有效托管您分配给其内容属性的任何内容。如果给定控件/页面上有一个区域可以保存不确定类型的动态内容,则 ContentPresenter 是保存该空间的有效方法。
它还经常与模板、自定义控件等一起使用。很可能在您开始接触一些相当高级的东西之前您不会真正使用它。
您可以做的一件很酷的事情是将 ContentPresenter 的 Content 属性绑定到 UserControl 类型的 DependencyProperty,然后如果您将该 DependencyProperty 设置为等于任何 UserControl(例如您在 ViewModel 中新创建的 UserControl 或其他控件),它就会就会出现在那个地方。
You don't always need a ContentPresenter. It acts as a placeholder that will effectively host any content that you assign to its Content attribute. If you have an area on a given control/page that can hold dynamic content of an indeterminate type, a ContentPresenter is an effective way to hold the space.
It's also used quite a bit with templating, custom controls, etc. Odds are you won't actually use it until you start getting into some fairly advanced stuff.
One kind of cool thing you can do is have the Content attribute of the ContentPresenter bound to a DependencyProperty of type UserControl, and then if you set that DependencyProperty equal to any UserControl (like one that you new up in a ViewModel or something), it'll show up in that spot.
来自 MSDN;所以基本上它是模板中内容的占位符。
From MSDN; so basically it's a placeholder for content in a template.
它由ContentControl使用。在 ContentControl 的模板中,ContentPresenter 指示将放置实际内容的占位符。
来自 MSDN,
It is used by ContentControl. Inside ContentControl's template, a ContentPresenter indicates as a placeholder where the actual content will be placed.
From MSDN,