Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As an alternative approach, I would suggest binding the <code>AlternationCount</code> of your <code>ItemsControl</code> to the number of items in your collection (e.g. the <code>Count</code> property). This will then assign to each container in your <code>ItemsControl</code> a unique <code>AlternationIndex</code> (0, 1, 2, ... Count-1). See here for more information:</p> <p><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.alternationcount.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.alternationcount.aspx</a></p> <p>Once each container has a unique <code>AlternationIndex</code> you can use a <code>DataTrigger</code> in your container <code>Style</code> to set the <code>ItemTemplate</code> based off of the index. This could be done using a <code>MultiBinding</code> with a converter that returns <code>True</code> if the index is equal the count, <code>False</code> otherwise. Of course you could also build a selector around this approach as well. With the exception of the converter, this approach is nice since it is a XAML only solution. </p> <p>An example using a <code>ListBox</code>:</p> <pre><code>&lt;Window x:Class="WpfApplication4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib" xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:l="clr-namespace:WpfApplication4" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;Grid.Resources&gt; &lt;Collections:ArrayList x:Key="MyCollection"&gt; &lt;System:String&gt;Item One&lt;/System:String&gt; &lt;System:String&gt;Item Two&lt;/System:String&gt; &lt;System:String&gt;Item Three&lt;/System:String&gt; &lt;/Collections:ArrayList&gt; &lt;l:MyAlternationEqualityConverter x:Key="MyAlternationEqualityConverter" /&gt; &lt;Style x:Key="MyListBoxItemStyle" TargetType="{x:Type ListBoxItem}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Value="True"&gt; &lt;DataTrigger.Binding&gt; &lt;MultiBinding Converter="{StaticResource MyAlternationEqualityConverter}"&gt; &lt;Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ListBox}}" Path="Items.Count" /&gt; &lt;Binding RelativeSource="{RelativeSource Self}" Path="(ItemsControl.AlternationIndex)" /&gt; &lt;/MultiBinding&gt; &lt;/DataTrigger.Binding&gt; &lt;!-- Could set the ItemTemplate instead --&gt; &lt;Setter Property="Background" Value="Red"/&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Grid.Resources&gt; &lt;ListBox ItemsSource="{Binding Source={StaticResource MyCollection}}" AlternationCount="{Binding RelativeSource={RelativeSource Self}, Path=Items.Count}" ItemContainerStyle="{StaticResource MyListBoxItemStyle}" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>Where the converter might look something like:</p> <pre><code>class MyAlternationEqualityConverter : IMultiValueConverter { #region Implementation of IMultiValueConverter public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { if (values != null &amp;&amp; values.Length == 2 &amp;&amp; values[0] is int &amp;&amp; values[1] is int) { return Equals((int) values[0], (int) values[1] + 1); } return DependencyProperty.UnsetValue; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotSupportedException(); } #endregion } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload