I am trying to set the background for my ListboxItem with the help of the ValueConverter. But the background is not applied. On debugging I found that the value converter returns correct values. Am I missing something in my code?
Note: I don't want to use alternate index style
XAML:
<Style x:Key="listBoxItemAlternateStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="MinHeight" Value="20"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource AlternateIndexConverter}">
<MultiBinding.Bindings>
<Binding Path="IsVisible" />
<Binding Path="Index" />
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsVisible}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsVisible}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
Codebehind:
public class AlternateIndexConverter : System.Windows.Data.IMultiValueConverter
{
public static uint count = 0;
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool isVisible = bool.Parse(values[0].ToString());
uint index = uint.Parse(values[1].ToString());
if (index == 0)
count = 0;
if (isVisible && count % 2 == 0)
{
count++;
return "#C8C8C8"; //dark color
}
else if (isVisible && count % 2 == 1)
{
count++;
return "#E1E1E1"; //light color
}
else
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}