I have created a Windows Phone 8.1 run-time app.
I am using the ListView control.
I want to alternate the each background row color.
After searching I found this link a previous answer.
But this gives errors in the markup. For one thing there is no 'AlternationCount' property. I am assuming this is because it is not SilverLight but RT?
If anyone can send me a link as I am struggerling to find a simple example. even better a simple code example would be appreciated.
I know there's already a few good answers to this question but I just want to throw in one more idea which I think it's a bit harder to implement but easier to use.
This solution will need the help from the
ListView
'sItemContainerStyleSelector
and aBehavior
from Behavior SDK (XAML).Basically, this
AlternatingColorItemContainerStyleSelector
behavior I've created allows you to specify twoSolidColorBrush
colors. It encapsulates the logic of creating aItemContainerStyleSelector
with two differentStyle
s as well as assigning the correspondingSolidColorBrush
to eachStyle
.Once you have the behavior in place, to use it is extremely simple - I only needed to drag and drop it onto the
ListView
in Expression Blend and specify two colors and that's it!Here's the behavior.
One thing to note is that removing items won't update all the other items' colors (simply because
SelectStyleCore
of other items won't be called), adding items will. But in your case this should be enough.Thanks for both your replies - much appreciated. I have another solution I would like to propose and I post it here for people to comment on.
EDIT - by Romasz
You can always play in the code if you want, but then your solution is not so universal, will have to be run always when you change collection and may have other problems. I placed this comment as an edit to your answer, hence the code above may be simplified and it can look like this (looks more nice in the answer):
My proposal is to use a Converter class with additional DependencyProperties. When you intialize the converter, you define to which items collection it will refer and a list of alternate brushes for a background. It can look for example like this:
Once you have it defined and create list of alternate brushes:
You can use it like this:
This solution should work, though it may have problem when you have IList of value types. Also here shouldn't be problems with deferred creation, as it retrives the index directly from list.
WPF is the only framework that supports "AlternationCount" -- neither Windows Phone nor Silverlight nor RT have it.
You might find that the easiest solution is just to add an "Index" or "IsOdd" property to your row model. The you can just bind to that property, using a converter to return the appropriate brush/color depending on the index.
One simpler approach is to bind to a converter that keeps track of the index using a static variable, like below. However, if you use item virtualization (which you probably do unless you have only a few items), then this approach runs into glitches: the deferred-creation of the UI elements results in indices getting assigned out-of-order, and you end up with consecutive rows showing same color.
For me the sleekest way to do it is:
Just hook you up to the ContainerContentChanging-Event of your ListView. I don't know if it works when your resort your list but for normal case it works very well.
You can even implement your own ListView, so you can use it as much as you like. With the right propertys you can also edit it in the xaml file. For example asigning #FFFF0000 (ARGB).