我有以下ItemsControl,我使用画布作为面板:
<ItemsControl ItemsSource="{Binding Widgets}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type widgetLayoutSpike:ColouredWidget}">
<Grid Background="{Binding BgColour}">
<TextBlock Text="{Binding Title}" />
</Grid>
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid Background="Yellow">
<!-- <ContentPresenter /> -->
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>我的要求是:
画布为每个绑定项创建一个ContentPresenter。正如您在上面看到的,我本来希望能够在ContentTemplate中为ContentPresenter指定一个ItemContainerStyle,但这是行不通的,因为我假设它实际上创建了一个循环引用。
提前感谢!
发布于 2013-08-27 11:07:59
使用ListBox而不是ItemsControl可能更容易做到这一点,因为容器类型是ListBoxItem,而ListBoxItem(与ContentPresenter相反)有一个控制模板,您可以在样式中替换该模板:
<ListBox ItemsSource="{Binding Widgets}">
<ListBox.Resources>
<DataTemplate DataType="{x:Type widgetLayoutSpike:ColouredWidget}">
<Grid Background="{Binding BgColour}">
<TextBlock Text="{Binding Title}" />
</Grid>
</DataTemplate>
</ListBox.Resources>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Background="Yellow">
<ContentPresenter Margin="2"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>编辑:也许你必须写
<ContentPresenter Margin="2"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"/>https://stackoverflow.com/questions/18462512
复制相似问题