I am trying to give alternate row style in listbox in window phone 7.
I have used the below: It worked if i have few rows.
<ListBox x:Name="ListBox1" Width="500" Height="300">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="500" Background="{Binding age,Converter={StaticResource myconverter}}">
<TextBlock Text="Some text"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and this is my converter which changing the background color:
public class MyConverter : IValueConverter
{
bool flag = false;
SolidColorBrush brush1 = new SolidColorBrush(Color.FromArgb(255, 100, 200, 255));
SolidColorBrush brush2 = new SolidColorBrush(Color.FromArgb(255, 200, 100, 155));
public object Convert(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
flag = !flag;
return flag ? brush1 : brush2;
}
public object ConvertBack(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
After binding it has given alternate row background color to my listbox . But if listbox have too many rows when i scroll it Up and Down very fastly then row color changing because it is calling Converter once again while scrolling listbox.
How can i fix this, Please help me?