Wpf Prism为ItemControl的每个项目赋予样式(区域中的视图)

时间:2015-07-14 11:19:33

标签: c# wpf prism

我有一个WPF Prism项目,Region基于ItemControl基础:

<ItemsControl prism:RegionManager.RegionName="WorkspaceRegion" >

在此ItemControl中,我看到了ViewsItem个,但我希望为每个ItemControl 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> )提供一种风格。

  

所有项目(视图)必须具有相同的样式(例如:背景   颜色,填充,边距,边框和...)

我想要这样的东西(例如):

enter image description here

我使用了一个简单的样式和代码:

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

错误:

  

用于类型&#39; ItemsControl&#39;的样式。无法应用于类型   &#39;视图1&#39;

我也测试了这些代码:

<ItemsControl prism:RegionManager.RegionName="WorkspaceRegion" >

但结果就像我写的那样:

<ItemsPresenter/>

enter image description here

  • 为什么呢?我的错是什么?

  • 我该怎么做?

EDIT1:

我使用<ContentPresenter Content="{Binding}"/>代替ControlTemplate

结果:没有任何改变

EDIT2:

我为ItemsControl的ItemContainerStyle属性编写了这个样式,如果,我将Presenter部分删除它。

现在的问题是Xaml TagControlTemplate我应该在Views UserControls <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> 中使用哪种try{ // your code for downloading from internet } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } private class DownloadJSON extends AsyncTask<String, Integer, String> { protected void onPreExecute() { } protected String doInBackground(String... urls) { String result = null; try { result = tryGetJSON(); } catch (IOException e) { e.printStackTrace(); } return result; } protected void onPostExecute(String result) { if (result != null) { //parse json } else{ //execute LocalDownloadJSON } } 所示。

public class Schedule
{
    [DisplayName("Build Time")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:hh\:mm}")]
    public TimeSpan Build_Time { get; set; }

    [DisplayName("Start Time")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:hh\:mm}")]
    public TimeSpan Call_Start_Time { get; set; }

    [DisplayName("End Time")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:hh\:mm}")]
    public TimeSpan Call_End_Time { get; set; }    

2 个答案:

答案 0 :(得分:1)

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

ItemsControl的呈现方式如下:

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

        <ContentPresenter>
            <ItemTemplate>
        </ContentPresenter>

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

ItemContainerStyle适用于包装此XAML树中每个项目的ContentPresenter对象,我认为ContentPresenter不具有Control.Template或{ {1}}财产。

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

答案 1 :(得分:0)

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

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