Wpf Prism 为 ItemControl(区域中的视图)的每个项目提供样式 [英] Wpf Prism give style to each items of a ItemControl (Views in a Region)
问题描述
我有一个 WPF
Prism
项目,它有一个基于 ItemControl
的 Region
:
I have a WPF
Prism
project and it has a Region
base on ItemControl
:
<ItemsControl prism:RegionManager.RegionName="WorkspaceRegion" >
在这个 ItemControl
中,我可以很好地看到我的一些 Views
,但我想为 ItemControl<的每个
Item
赋予一个样式/code>(每个 View
).
In this ItemControl
i see some of my Views
verticaly well but i want give a style to each Item
of ItemControl
(each View
).
所有项目(视图)必须具有相同的样式(例如:背景颜色、内边距、边距、边框和...)
All of items (views) must have same style (for example: background color, padding, margin, border and...)
我想要这样的东西(例如):
I want something like this (for example):
我使用了这样一个简单的样式和代码:
I used a simple style and code like this:
<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'
A style intended for type 'ItemsControl' cannot be applied to type 'View1'
我也测试了这个代码:
<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" >
为什么?我的错误是什么?
Why? What is my mistake?
我该怎么做?
编辑 1:
我使用了 <ItemsPresenter/>
而不是 <ContentPresenter Content="{Binding}"/>
结果:没有任何变化
编辑 2:
我为 ItemsControl 的 ItemContainerStyle 属性编写了此样式,并且它可以如果我从中删除 ControlTemplate
部分.
I write this style for ItemContainerStyle property of the ItemsControl and it works if i remove ControlTemplate
part from it.
现在的问题是我应该在以下 ControlTemplate
中使用哪种 Presenter
或 Xaml Tag
到我的 视图
(UserControls
) 被显示.
Now the question is which kind of Presenter
or Xaml Tag
i should use inside the following ControlTemplate
to my Views
(UserControls
) be shown.
<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>
推荐答案
设置样式的 ContentTemplate
属性,而不是 Control.Template
或 Template代码>
Set the ContentTemplate
property of the style, not Control.Template
or Template
一个 ItemsControl 被渲染成这样:
An ItemsControl gets rendered like this :
<ItemsControl>
<ItemsPanel>
<ContentPresenter>
<ItemTemplate>
</ContentPresenter>
<ContentPresenter>
<ItemTemplate>
</ContentPresenter>
<ContentPresenter>
<ItemTemplate>
</ContentPresenter>
</ItemsPanel>
</ItemsControl>
ItemContainerStyle
适用于包装此 XAML 树中每个项目的 ContentPresenter
对象,我不相信 ContentPresenter
Control.Template
或 Template
属性.
The ItemContainerStyle
applies to the ContentPresenter
object that wraps each item in this XAML tree, and I don't believe a ContentPresenter
has either a Control.Template
or a Template
property.
当更改 ContentPresenter
的显示方式时,您应该改写 ContentTemplate
属性.
When changing how a ContentPresenter
is displayed you should overwrite the ContentTemplate
property instead.
这篇关于Wpf Prism 为 ItemControl(区域中的视图)的每个项目提供样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!