如何在ItemsControls中使用AlternationIndex?

Fil*_*kun 27 wpf styles itemscontrol mvvm

我已经看到一些文章,展示了如何使用AlternationIndexListBoxES或ListViewS,但我花了几个小时试图让基交替背景色ItemsControl类和似乎没有任何工作.ListBox我看到的所有样本都ListBoxItem用作设置背景的样式的目标类型AlternationIndex- 就像MSDN中的这样:

<Grid>
    <Grid.Resources>
        <Style x:Key="alternatingWithTriggers" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="Blue"/>
            <Setter Property="Foreground" Value="White"/>
            <Style.Triggers>
                <Trigger Property="ListBox.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="CornflowerBlue"/>
                    <Setter Property="Foreground" Value="Black"/>
                </Trigger>
                <Trigger Property="ListBox.AlternationIndex" Value="2">
                    <Setter Property="Background" Value="LightBlue"/>
                    <Setter Property="Foreground" Value="Navy"/>
                </Trigger>
            </Style.Triggers>
        </Style>

    </Grid.Resources>
    <ListBox AlternationCount="3" ItemsSource="{StaticResource data}" 
             ItemContainerStyle="{StaticResource alternatingWithTriggers}">
    </ListBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)

我想使用它,ItemsControl因为我不想要选择功能,我认为重新设置a ListBox来隐藏它可能不是最好的选择.

这是我尝试的事情之一:

<DataTemplate DataType="{x:Type vm:ObservableCollectionItem}">
    <Grid>
        <!-- some content here -->
    </Grid>
</DataTemplate>

<!-- ... -->

<ItemsControl
    ItemsSource="{Binding ObservableCollectionItems}"
    AlternationCount="2"
>
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Grid.Background" Value="Red"></Setter>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Grid.Background" Value="Blue"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

我看到的问题是可视树有一个ContentPresenters 列表,它们ItemsControl.AlternationIndex在0和1之间交替,但Grid每个ContentPresenterItemsControl.AlternationIndex设置为0.

可能有一些我很想念的东西......

Bub*_*rap 47

ItemContainerStyle应用于ItemsControl:ContentPresenter生成的元素.ContentPresenter将包含您在ItemTemplate中放置的任何内容.对于ListBox,ItemContainerStyle应用于生成的ListBoxItem.

AlternationCount基于您发布的内容,仅适用于这些生成的项目.您不能使用ItemContainerStyle来设置网格的背景,因为该样式不知道网格.

以下是理想的,但不幸的是ContentPresenter没有背景属性.但它适用于ListBox(带有ListBoxItems).

<ItemsControl
    ItemsSource="{Binding ObservableCollectionItems}"
    AlternationCount="2">
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="Red"></Setter>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="Blue"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

因此,您最终会为网格编写一个样式,该样式将绑定到父ContentPresenter的AlternationIndex.

<DataTemplate DataType="{x:Type vm:ObservableCollectionItem}">
    <Grid>
        <Grid.Style>
            <Style TargetType="Grid">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="0">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Path=(ItemsControl.AlternationIndex)}" Value="1">
                        <Setter Property="Background" Value="Blue"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
    </Grid>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)


Nac*_*mpi 27

..玩了大约2个小时后,我终于找到了简单有效的解决方案:

       <ItemsControl ItemsSource="{Binding}" AlternationCount="2">
              <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid Background="Transparent" x:Name="__PART_GRID"></Grid>
                        <DataTemplate.Triggers>
                            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                                <Setter TargetName="__PART_GRID" Property="Background" Value="Red"/>
                            </Trigger>
                            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                <Setter TargetName="__PART_GRID" Property="Background" Value="Blue"/>
                            </Trigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
         </ItemsControl>
Run Code Online (Sandbox Code Playgroud)

我希望这个答案可以帮助别人节省一些时间.


小智 5

或者,正如我在另一篇文章中发现的那样,它对我很有用……您可以简单地使用绑定……

{Binding
    RelativeSource={RelativeSource Mode=TemplatedParent}, 
    Path=(ItemsControl.AlternationIndex)}
Run Code Online (Sandbox Code Playgroud)

注意:记得在你的 ItemsControl 上添加 AlterationCount="100"