Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf Can';不能同时使用ItemTemplate和ItemContainerStyle吗?_Wpf - Fatal编程技术网

Wpf Can';不能同时使用ItemTemplate和ItemContainerStyle吗?

Wpf Can';不能同时使用ItemTemplate和ItemContainerStyle吗?,wpf,Wpf,我正在尝试将ItemTemplate和ItemContainerStyle应用于ItemsControl:- <ItemsControl ItemsSource="{Binding LogEntries}" ItemTemplate="{StaticResource itemTemplate}" ItemContainerStyle="{StaticResource itemContainer}" /> 然而,ItemCo

我正在尝试将ItemTemplate和ItemContainerStyle应用于ItemsControl:-

<ItemsControl ItemsSource="{Binding LogEntries}"
              ItemTemplate="{StaticResource itemTemplate}"
              ItemContainerStyle="{StaticResource itemContainer}" />

然而,ItemContainerStyle似乎被忽略了(但是如果我删除ItemTemplate,它确实可以工作)

项目模板相当复杂,在许多不同的视图中使用。在一个特定视图中,我需要更改列表项的间距和背景颜色,因此我也尝试应用ItemContainerStyle,如下所示:-

<Style x:Key="itemContainer"
       TargetType="ContentPresenter">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Border x:Name="itemBorder"
                        Margin="4,0,4,4"
                        Background="#666666">
                    <ContentPresenter Content="{Binding}" />
                </Border>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>


我有点惊讶你不能同时申请,除非我遗漏了什么?我假设ItemContainerStyle实际上只是一个围绕项目内容的“包装器”,而不管项目内容是否是模板化的?

ItemContainerStyle不是任何东西的“包装器”。。。这是一种
风格
。您可以设置项目容器的
Style
ItemTemplate
属性,但问题的原因是您试图在
Style
中设置
ContentPresenter
ContentTemplate
属性,而这会被
ItemTemplate
的值覆盖。(见评论部分的@Clemens'链接)

解决此问题的一种方法是使用
ListBox
将其数据项包装在
ListBoxItem
s中,并为
Template
属性而不是
ContentTemplate
提供值。(当然,您可以添加
样式
以删除其边框,使其看起来像
项控件
)。在这种情况下,
ItemContainerStyle
将影响
ListBoxItem
。然而,你必须理解其中的区别


ItemContainerStyle
将影响
ListBoxItem
,而
ItemTemplate
用于定义其中的数据对象。因此,在
ItemContainerStyle
中定义
边框
,并在
ItemTemplate
中定义数据的外观是合适的。试试这个:

<ListBox ItemsSource="{Binding Items}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border Margin="4,0,4,4" Background="#666666">
                            <ContentPresenter Content="{Binding}" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

如果您阅读MSDN页面上的备注,尤其是第2点,就会变得清晰。