0

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();
}

}

enter image description here

4

1 回答 1

2

Brush从你的Converter而不是返回String

 if (isVisible && count % 2 == 0)
    {
        count++;
        return new SolidColorBrush(Color.FromArgb(255,200,200,200)); //dark color
    }
    else if (isVisible && count % 2 == 1)
    {
        count++;
        return new SolidColorBrush(Color.FromArgb(255,225,225,225)); //light color
    }
于 2013-10-08T10:39:10.233 回答