WPF DataTemplate ContentPresenter “绑定”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22504291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataTemplate ContentPresenter "Binding"
提问by Bador
Can someone explain me how the code I am using here can work?
有人可以解释一下我在这里使用的代码是如何工作的吗?
<Window.Resources>
<DataTemplate DataType="{x:Type VM:PBRKEntryViewModel}">
<V:Overview />
</DataTemplate>
<DataTemplate DataType="{x:Type VM:LoginViewModel}">
<V:LoginView />
</DataTemplate>
</Window.Resources>
<Grid>
<ContentPresenter Content="{Binding CurrentView}"/>
</Grid>
My current problems in Details are:
我目前在细节方面的问题是:
- Why can the ContentPresenter present the correct UserControl without Reference to the different DataTemplates? I can see, that ContentPresenter content is bound to my ViewModels CurrentViewProperty but my DataTemplates not?
- Another great feature is that the UserControls using the correct ViewModels without a declaration. (Or without a declaration I can see)
- 为什么 ContentPresenter 可以在不引用不同 DataTemplate 的情况下呈现正确的 UserControl?我可以看到,ContentPresenter 内容绑定到我的 ViewModels CurrentViewProperty 但我的 DataTemplates 没有?
- 另一个很棒的功能是 UserControls 使用正确的 ViewModels 而没有声明。(或者没有我可以看到的声明)
I have found this description http://msdn.microsoft.com/en-us/library/System.Windows.Controls.ContentPresenter(v=vs.110).aspxbut the remarks section has no answer to this questions. (Or I couldn′t see them...)
我找到了这个描述http://msdn.microsoft.com/en-us/library/System.Windows.Controls.ContentPresenter(v=vs.110).aspx但备注部分没有回答这个问题。(或者我看不到他们......)
Again and just for clarity everything is working perfect, but I do not understand why, so this is just a question to understand the Selection of the template and the Binding.
再次,为了清楚起见,一切都运行良好,但我不明白为什么,所以这只是一个理解模板选择和绑定的问题。
回答by GazTheDestroyer
DateTemplates that specify a DataTypeproperty are automatically applied to any instance of that type in the view. It's just a way to tell WPF "every time you need to display this type, use this template"
指定DataType属性的DateTemplate会自动应用于视图中该类型的任何实例。这只是告诉WPF“每次需要显示这种类型时,使用这个模板”的一种方式
Your ContentPresenterhas its Contentbound to some object. If that object type has a matching template, then WPF will use it.
你ContentPresenter有它的Content绑定到某个对象。如果该对象类型具有匹配的模板,则 WPF 将使用它。
回答by Rohit Vats
Under the remarks section of the link you posted it's clear enough with this statement:
在您发布的链接的备注部分下,此声明已足够清楚:
If there is a DataTemplate associated with the type of Content, the ContentPresenter applies that DataTemplate to the Content property and the resulting UIElement and its child elements, if any, are displayed.
如果存在与 Content 类型关联的 DataTemplate,则 ContentPresenter 将该 DataTemplate 应用于 Content 属性,并显示生成的 UIElement 及其子元素(如果有)。
Also, if you want to know how dataTemplates are picked automatically, you can read about it here - Data Templating Overview.
此外,如果您想知道如何自动选择 dataTemplates,您可以在此处阅读有关它的信息 -数据模板概述。
Quote from the link:
引自链接:
The DataTemplate class has a DataType property that is very similar to the TargetType property of the Style class. DataTemplate gets applied automatically to all objects associated with underlying type.
DataTemplate 类有一个 DataType 属性,它与 Style 类的 TargetType 属性非常相似。DataTemplate 自动应用于与基础类型关联的所有对象。
This is something similar to Styles. If you doesn't specify any x:Keyon your Style it will be applied automatically to all child elements falling under the root element where resource is defined.
这类似于 Styles。如果您没有x:Key在 Style 上指定任何内容,它将自动应用于定义资源的根元素下的所有子元素。
As soon as you set x:Keyon Style, it is no more a default style and will be applied only to the elements explicitly setting style to this resource.
一旦您设置x:Key了样式,它就不再是默认样式,只会应用于为该资源显式设置样式的元素。
Same holds true for DataTemplateas well. When you specify DataTypeonly, it becomes default template to represent underlying data type. Explicitly specifying x:Keywill break this feature.
同样适用于DataTemplate。当您DataType只指定时,它成为表示底层数据类型的默认模板。显式指定x:Key将破坏此功能。

