wpf - ContentPresenter.ContentSource与内容

标签 wpf xaml controltemplate contentpresenter

基本上,我不了解这里的真正区别是:

TabItem的Microsoft代码使用:

<ContentPresenter ContentSource="Header" ... />


那么,什么时候可以使用Content属性代替(或补充使用)ContentSource

最佳答案

此属性仅在以下情况下使用
ContentPresenter在模板中。
当模板包含
具有ContentSource的ContentPresenter
设置为“ Abc”,“内容”
ContentTemplate,以及
ContentTemplateSelector的属性
ContentPresenter会自动
别名为Abc,AbcTemplate和
分别为AbcTemplateSelector。
从.NET Framework开始
版本3.5 Service Pack 1,设置
ContentSource设为“ Abc”也会导致
ContentStringFormat属性为
别名为AbcStringFormat。

这两个最有用的值
属性是“内容”和“标题”。


MSDN

ContentSource为了方便起见,显然一次设置了更多属性。



实际上,声明:

<ContentPresenter ContentSource="Header" />


执行以下初始化。

<ContentPresenter Content="{TemplateBinding Header}"
                  ContentTemplate="{TemplateBinding HeaderTemplate}"
                  ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                  ContentStringFormat="{TemplateBinding HeaderStringFormat}" />


仅当模板控件上存在依赖项属性时,它才分别对每个属性执行此操作。

关于wpf - ContentPresenter.ContentSource与内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5600221/

相关文章:

c# - Unity CS0308 错误

c# - 将命令绑定(bind)到代码隐藏中的 KeyBinding

c# - .NET Native 比使用 ReadAsync 调用的调试构建慢得多

c# - 将自定义依赖属性绑定(bind)到自定义 WPF 样式

wpf - 为什么x :key required on ControlTemplate in the ResourceDictionary

wpf - 分层数据、catel 和 MVVM

.net - 使用多重绑定(bind)绑定(bind)宽度属性会导致宽度为零

c# - WPF 工具栏删除放置在其中的控件的自定义样式

c# - MVVM 中的绑定(bind)错误

Wpf 模板库