I have a StackPanel that I dynamically hide/show items from, and I would like to make it so that the background property of each item alternates (white and grey). I tried doing it programmatically, but it is quite inefficient as the code that sets the background of each item runs every time an item is hidden/shown. I know that maybe using Styles/Templates could solve this, but for some reason this particular feature of WPF has confused me every time I try to learn it. Could someone please show me how to do this?
Also, I have thought of using ListBox/ListViews rather than StackPanel, but trust me that due to my particular implementation they don’t work as well.
You can do it with any ItemsControl using triggers on attached property ItemsControl.AlternationIndex (sample – http://dotnetbutchering.blogspot.com/2009/08/how-to-set-wpf-listview-alternate-row.html) but i don’t think it is possible in xaml only with StackPanel. Are you sure doing it programatticaly is slow? I am quite sure it will work fast enough so you don’t need to bother.