【问题标题】:WPF: Alternating colors on a ItemsControl?WPF:在 ItemsControl 上交替颜色?
【发布时间】:2011-05-23 05:12:37
【问题描述】:

如何在 ItemsControl 上获得交替颜色?我将 AlternationCount 设置为 2,但 ItemsControl.AlternationIndex 属性始终返回 0。

        <ItemsControl ItemsSource="{Binding}" AlternationCount="2">
            <ItemsControl.Resources>
                <Style x:Key="FooBar" TargetType="Grid">
                    <Style.Triggers>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                            <Setter Property="Background" Value="Blue"/>
                        </Trigger>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                            <Setter Property="Background" Value="Red"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ItemsControl.Resources>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="0,0,0,10" Style="{StaticResource FooBar}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="25" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions >
                            <RowDefinition Height="Auto" />
                            <!--<RowDefinition Height="Auto" />-->
                        </Grid.RowDefinitions>

                        <CheckBox IsChecked="{Binding Checked, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" />
                        <Label Grid.Column="1" Content="{Binding CompanyName}" />
                        <Label Grid.Column="2" Content="{Binding TradeKey}" />
                        <Label Grid.Column="3" Content="{Binding TradeDate}" ContentStringFormat="d" />
                        <Label Grid.Column="4" Content="{Binding Cusip}" />
                        <Label Grid.Column="5" Content="{Binding IssueName}" />
                        <Label Grid.Column="6" Content="{Binding TotalUnits}" ContentStringFormat="N0" />

                        <!--<Expander Grid.Row="0" Grid.Column="7" Grid.ColumnSpan="7" IsExpanded="True">
                            <Expander.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Label Content="Allocations"/>
                                    <Button Content="Edit" Name="cmdEdit" Click="cmdEdit_Click"  />
                                </StackPanel>
                            </Expander.Header>-->
                        <DataGrid Grid.Column="7" IsReadOnly="True" ItemsSource="{Binding Territories}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Rep on Ticket" Binding="{Binding TradeCustomer.RepNameNotes}" />
                                <DataGridTextColumn Header="Rep # on Ticket" Binding="{Binding TradeCustomer.RepNumberNotes}" />
                                <DataGridTextColumn Header="State" Binding="{Binding TradeCustomer.AccountStateKey}" />
                                <DataGridTextColumn Header="Qty. on Ticket" Binding="{Binding TradeCustomer.Quantity, StringFormat=N0}" />

                                <DataGridTextColumn Header="Zip Code" Binding="{Binding ZipCode}" />
                                <DataGridTextColumn Header="State" Binding="{Binding State}" />
                                <DataGridTextColumn Header="Territory" Binding="{Binding Territory}" />

                            </DataGrid.Columns>

                        </DataGrid>
                        <!--</Expander>-->

                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.Template>
                <ControlTemplate>
                    <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" SnapsToDevicePixels="True">
                        <ScrollViewer Padding="{TemplateBinding Control.Padding}" Focusable="False">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </ItemsControl.Template>
        </ItemsControl>

【问题讨论】:

标签: wpf styles itemscontrol


【解决方案1】:

在这里查看http://www.codeproject.com/Articles/35886/WPF-ItemsControl-with-alternating-items-and-hover-.aspx

您必须像这样更改代码才能使其正常工作

    <ItemsControl ItemsSource="{Binding DataList}" AlternationCount="2">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="FooBar" Margin="0,0,0,10">                    
                   ----------------------------
                   ----------------------------
                </Grid>
                <DataTemplate.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="Background" Value="Blue" TargetName="FooBar"/>
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="Red" TargetName="FooBar"/>
                    </Trigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ItemsControl.ItemTemplate>

【讨论】:

  • 啊,我现在看到了不同。谢谢。
  • 只是想叫它工作,ItemsControl.AlternationCount 也必须设置为 2 -- 让我绊倒了几分钟
  • 起初这对我不起作用,但后来我发现了 Property AlternationCount 的使用,当然必须将其设置为大于 1 的整数才能看到任何交替。感谢 biju 提供这么好的样品。
  • 如果您想将其用于TreeView,您需要将触发器复制到TreeView.ItemContainerStyle。否则它对我不起作用。
【解决方案2】:

这是一个可能更通用的替代方法

<DataTemplate x:Key="AlternatingTemplate">
    <Border>
        <Border.Style>
            <Style TargetType="{x:Type Border}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), 
                                                   RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 
                                 Value="0">
                        <Setter Property="Background" Value="White" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), 
                                                   RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 
                                 Value="1">
                        <Setter Property="Background" Value="LightGray" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
        <ContentPresenter Content="{Binding}" />
    </Border>
</DataTemplate>

用法:

<ItemsControl AlternationCount="2" 
              ItemTemplate="{StaticResource AlternatingTemplate}" 
              ItemsSource="{Binding SourceOfData}" />

【讨论】:

    【解决方案3】:

    如果您不想使用DataTemplate 方法,您可以创建一个使用ContentControl 作为项目容器的自定义控件,从而允许您指定背景颜色。

    类:

    public class ItemsControlAlternating : ItemsControl
    {
        static ItemsControlAlternating()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ItemsControlAlternating),
                     new FrameworkPropertyMetadata(typeof(ItemsControlAlternating)));
        }
    
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new ContentControl();
        }
    
        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is ContentControl;
        }
    }
    

    资源字典:

    <Style TargetType="{x:Type c:ItemsControlAlternating}">
       <Setter Property="AlternationCount" Value="2"/>
       <Setter Property="Template">
           <Setter.Value>
               <ControlTemplate TargetType="{x:Type c:ItemsControlAlternating}">
                   <ItemsPresenter/>
               </ControlTemplate>
           </Setter.Value>
       </Setter>
       <Setter Property="ItemContainerStyle">
           <Setter.Value>
               <Style TargetType="{x:Type ContentControl}">
                   <Setter Property="Template">
                       <Setter.Value>
                           <ControlTemplate TargetType="{x:Type ContentControl}">
                               <Border Background="{TemplateBinding Background}">
                                   <ContentPresenter/>
                               </Border>
                           </ControlTemplate>
                       </Setter.Value>
                   </Setter>
                   <Style.Triggers>
                       <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                           <Setter Property="Background" Value="Gray"/>
                       </Trigger>
                       <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                           <Setter Property="Background" Value="White"/>
                       </Trigger>
                   </Style.Triggers>
               </Style>
           </Setter.Value>
       </Setter>
    </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 2011-07-04
      • 2020-11-29
      • 2010-09-20
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多