我有ListView
我想在其中的项目中应用自定义的ControlTemplate
。它是这样定义的:WPF ControlTemplate AND DataTemplate
<ListView ItemsSource="{Binding MyAwesomeItems}" ...
MyAwesomeItems包含不同的类。所以我想:“好吧,你好DataTemplates。”
为了使包含的项目看我希望他们的样子,我已经定义了一个ControlTemplate
这样的:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border><ContentControl Content="{TemplateBinding Content}"/></Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
我已经使用ContentControl
与绑定到TemplateBinding Content
。我预计WPF会使用我为它定义的任何DataTemplate将我的项目插入ContentControl
。
但相反,它看起来像WPF只是使用项目.ToString()
并不适用任何DataTemplates。这是预期的行为?
我想达到的目标是:有一个项目,每个项目的容器看起来正是我想要的方式和内容容器来自DataTemplate中的列表。
改为使用ContentPresenter。区别在于如何设置'DataContext'。 –
没关系。同样的结果。 – Hemisphera
我忘了提及:不要设置“内容”!只需使用 。 –