Aracılığıyla paylaş


GroupStyle.AlternationCount Özellik

Tanım

Değişen GroupItem nesnelerin sayısını alır veya ayarlar.

public:
 property int AlternationCount { int get(); void set(int value); };
public int AlternationCount { get; set; }
member this.AlternationCount : int with get, set
Public Property AlternationCount As Integer

Özellik Değeri

Değişen GroupItem nesnelerin sayısı.

Örnekler

Aşağıdaki örnek, koleksiyondaki öğeleri gruplandıran bir ile ilişkili bir CollectionViewSource oluştururListBox. Örnek, aşağıdakileri yaparak her GroupItem birinin üst bilgisinin arka planını iki renk arasında alternatif yapar:

Örnek ayrıca öğesini ve her birinin öğesini öğesine bağlayarak AlternationCountBackground içindeki öğelerin ListBox arka planını üç renk arasında değiştirmektedirItemsControl.AlternationIndex.ListBoxListBox Bu durumda arka plan üç renk arasında değiştirilir.

Öğeleri CollectionViewSource gruplandıran öğe gösterilmez. Öğeyi gruplandırma hakkında bilgi için bkz . Nasıl yapılır: XAML'de Görünüm Kullanarak Verileri Sıralama ve Gruplandırma.

<StackPanel>

  <StackPanel.Resources>

    <!--Returns a Brush for the header of a GroupItem.-->
    <AlternationConverter x:Key="GroupHeaderBackgroundConverter">
      <SolidColorBrush>LightBlue</SolidColorBrush>
      <SolidColorBrush>LightSteelBlue</SolidColorBrush>
    </AlternationConverter>

    <!--Returns a Brush for a ListBoxItem.-->
    <AlternationConverter x:Key="BackgroundConverter">
      <SolidColorBrush>Silver</SolidColorBrush>
      <SolidColorBrush>LightGray</SolidColorBrush>
      <SolidColorBrush>GhostWhite</SolidColorBrush>
    </AlternationConverter>

  </StackPanel.Resources>

  <ListBox ItemsSource="{Binding Source={StaticResource groupedData}}"
           DisplayMemberPath="CityName" AlternationCount="3" Name="lb">

    <ListBox.GroupStyle>
      <!--Set alternating backgrounds on the header of each group.-->
      <GroupStyle AlternationCount="2">
        <GroupStyle.HeaderTemplate>
          <DataTemplate>
            <TextBlock FontWeight="Bold" 
                       Text="{Binding Path=Name}" 
                       Background="{Binding 
                           RelativeSource={RelativeSource FindAncestor, 
                           AncestorType={x:Type GroupItem}},
                           Path=(ItemsControl.AlternationIndex),
                           Converter={StaticResource 
                                      GroupHeaderBackgroundConverter}}"/>
          </DataTemplate>
        </GroupStyle.HeaderTemplate>
      </GroupStyle>
    </ListBox.GroupStyle>

    <ListBox.ItemContainerStyle>
      <!--Set alternating backgrounds on the items in the ListBox.-->
      <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" 
                Value="{Binding RelativeSource={RelativeSource Self},
                     Path=(ItemsControl.AlternationIndex),
                     Converter={StaticResource BackgroundConverter}}"/>
      </Style>
    </ListBox.ItemContainerStyle>
  </ListBox>
</StackPanel>

Açıklamalar

AlternationCount özelliği ve ItemsControl.AlternationIndex ekli özelliği, iki veya daha fazla değişen GroupItem nesnenin görünümünü belirtmenizi sağlar. Örneğin, içindeki ItemsControlher üç GroupItem için değişen arka plan renkleri belirtebilirsiniz. ItemsControl.AlternationIndex içindeki her GroupItemItemsControlbirine atanır. ItemsControl.AlternationIndex 0'da başlar, eksi 1 olana AlternationCount kadar artar ve 0'da yeniden başlatılır. Örneğin, 3 ise AlternationCount ve içinde ItemsControlyedi GroupItem nesne varsa, aşağıdaki tabloda her öğe için öğesini ItemsControl.AlternationIndex listeler.

GroupItem konumuItemsControl ItemsControl.AlternationIndex
1 0
2 1
3 2
4 0
5 1
6 2
7 0

Alternatif GroupItem nesneler için farklı görünümler belirtmek için kullanabileceğiniz çeşitli yöntemler vardır. Bir yöntem, içindeki veya ContainerStyleGroupStyle içindeki HeaderTemplate özellikleri öğesine bağlamaktırItemsControl.AlternationIndex. Ardından, belirli ItemsControl.AlternationIndex bir değere sahip olan değerine hangi değerin GroupItem uygulanacağını belirtmek için bir AlternationConverter kullanabilirsiniz. Bir özelliğin değerini değerine bağlı ItemsControl.AlternationIndexolarak değiştirmek için tetikleyicileri de kullanabilirsiniz.

Şunlara uygulanır