【问题标题】:Wpf Prism give style to each items of a ItemControl (Views in a Region)Wpf Prism 为 ItemControl 的每个项目赋予样式(区域中的视图)
【发布时间】:2015-10-03 00:03:32
【问题描述】:

我有一个WPFPrism 项目,它有一个基于ItemControlRegion

<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:

我用&lt;ItemsPresenter/&gt;代替&lt;ContentPresenter Content="{Binding}"/&gt;

结果:没有任何变化


编辑2:

我为 ItemsControl 的 ItemContainerStyle 属性编写了这种样式,它可以工作如果我从中删除 ControlTemplate 部分。

现在的问题是我应该在下面的ControlTemplate 到我的Views (UserControls) 中使用哪种PresenterXaml 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 中每个项目的 &lt;ContentPresenter&gt; 项目。我不是很肯定,但我不认为ContentPresenterControl.Template 属性。尝试设置 ContentTemplate 属性。

标签: c# wpf prism


【解决方案1】:

设置样式的ContentTemplate属性,而不是Control.TemplateTemplate

ItemsControl 被渲染成这样:

<ItemsControl>
    <ItemsPanel>
        <ContentPresenter>
            <ItemTemplate>
        </ContentPresenter>

        <ContentPresenter>
            <ItemTemplate>
        </ContentPresenter>

        <ContentPresenter>
            <ItemTemplate>
        </ContentPresenter>
    </ItemsPanel>
</ItemsControl>

ItemContainerStyle 适用于包装此 XAML 树中每个项目的 ContentPresenter 对象,我不相信 ContentPresenter 具有 Control.TemplateTemplate 属性。

当更改 ContentPresenter 的显示方式时,您应该改写 ContentTemplate 属性。

【讨论】:

    【解决方案2】:

    这适用于我的测试应用程序:

    <ItemsControl Background="#FF85664F" prism:RegionManager.RegionName="WorkspaceRegion">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="DarkGray" Padding="10"
                        Margin="5, 5, 5, 0" BorderThickness="1"
                        CornerRadius="5" Background="#FFC3BF8F">
                    <ContentPresenter Content="{Binding}" />
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <system:String>ItemsControl Item #1</system:String>
        <system:String>ItemsControl Item #2</system:String>
        <system:String>ItemsControl Item #3</system:String>
        <system:String>ItemsControl Item #4</system:String>
        <system:String>ItemsControl Item #5</system:String>
    </ItemsControl>
    

    【讨论】:

    • 是的,它运行良好,但我的问题是 WPF 中的 Prism 项目。当您向其中添加 prism:RegionManager.RegionName="WorkspaceRegion" 并将您的项目移动到 prism 框架时,它将无法与 View 项目一起使用
    • 您在样本中手动插入了项目,并且您的项目类型是字符串,并且字符串类型与棱镜视图类型不同,那么这些是不同的。你的项目没有被查看!
    • @RAM 没错,我想必须在 ContentControl 中引用 View-Models 并希望 Prism 能够附上相应的View。然而,Prism 通常以相反的方式执行这些关联。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多