【发布时间】:2015-10-03 00:03:32
【问题描述】:
我有一个WPFPrism 项目,它有一个基于ItemControl 的Region:
<ItemsControl prism:RegionManager.RegionName="WorkspaceRegion" >
在这个ItemControl 中,我很好地看到了我的一些Views,但我想给ItemControl 中的每个Item 一个样式(每个View)。
所有项目(视图)必须具有相同的样式(例如:背景 颜色、内边距、边距、边框和...)
我想要这样的东西(例如):
我使用了这样一个简单的样式和代码:
<ItemsControl prism:RegionManager.RegionName="WorkspaceRegion" Background="#765e4d" Margin="10">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Red" Padding="10" BorderThickness="1" CornerRadius="5">
<ContentPresenter Content="{Binding}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
它的错误:
用于类型“ItemsControl”的样式不能应用于类型 '视图1'
我也测试了这个代码:
<ItemsControl prism:RegionManager.RegionName="WorkspaceRegion" Background="#765e4d" Margin="10">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid >
<TextBlock Text="Test"/>
<Border BorderBrush="Red" Padding="10" Margin="10" BorderThickness="1" CornerRadius="5">
<ContentPresenter Content="{Binding}"/>
</Border>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
但结果就像我写的时候一样:
<ItemsControl prism:RegionManager.RegionName="WorkspaceRegion" >
为什么?我的错误是什么?
我该怎么做?
编辑1:
我用<ItemsPresenter/>代替<ContentPresenter Content="{Binding}"/>
结果:没有任何变化
编辑2:
我为 ItemsControl 的 ItemContainerStyle 属性编写了这种样式,它可以工作如果我从中删除 ControlTemplate 部分。
现在的问题是我应该在下面的ControlTemplate 到我的Views (UserControls) 中使用哪种Presenter 或Xaml Tag。
<Style TargetType="{x:Type UserControl}" x:Key="MyItemContainerStyle">
<Setter Property="Background" Value="Brown"/>
<Setter Property="BorderBrush" Value="Blue"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Margin" Value="10"/>
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate>
??????????????????????????
<!-- The following ContentPresenter not working and the Items dose not show -->
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
【问题讨论】:
-
我是否理解正确:例如,您的 ItemsCollection 中有 3 个项目,并且您希望每个项目都有一种样式,而不是所有项目的一种样式?
-
@mDC 是的,但所有项目的一种风格。我认为该区域的每个视图都显示在 ItemsControl 的一个项目中,我想在每个项目上应用一个样式。所有项目(视图)必须具有相同的样式。
-
您能否提供一张图片,实际结果是什么?这将帮助我理解:)
-
@mDC,我添加了我的 Shell 的图片。您可以看到没有一个视图具有任何样式,例如边框或填充或边距......
-
ItemContainerStyle应用于包装 ItemsControl 中每个项目的<ContentPresenter>项目。我不是很肯定,但我不认为ContentPresenter有Control.Template属性。尝试设置ContentTemplate属性。