Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to Alternate color in ItemsControl without touching the (default/global) DataTemplate

$
0
0

Hi  there,

trying to grasp a fine difference:

have a default DataTemplate

<DataTemplate DataType="{x:Type local:MyType}">
  <Grid>
...here some cool stuff :)...
  </Grid
</DataTemplate>

have an ItemsControl

<ItemsControl ItemSource="{Binding some datalist of local:MyType}" AlternationCount="2">
  <ItemsControl.ItemContainerStyle>
    <Style>
      <Style.Triggers>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
          <Setter Property= "how to set Background on Grid in DataTemplate"
                       Value="LightBlue"/>
        </Trigger>
      </Style.Triggers>
   </Style>
  </ItemsControl.ItemContainerStyle>
</ItemsControl>

As far as I've understood <ItemsControl.ItemContainerStyle/> would be the place to apply styles that style specific visual behaviours in respect to an ItemsControl (e.g. alternating line color).

I'm experiencing problems achieving my goal.

If I'd define the AlternationIndex-dependent coloring within theDataTemplate I could get it to work.

But somehow I believe that isn't the way it should be done.

Maybe some bright programmer can show me how to keep my DataTemplate "clean":

  • no ItemsControl.AlternationIndex-reference in DataTemplate,
  • pure xaml,
  • styling within <ItemsControl.ItemConstainerStyle/>

and still be able to alternate-color lines in an ItemsControl.

BTW, I've read how not to solve my problem because DataTemplate is adapted to ItemsControl (exactly what I am trying to prevent)

Thnx.


If you don't dig, you won't find it



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>