简体   繁体   中英

ListBoxItem style query

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

}

在此处输入图片说明

Return Brush from your Converter instead of 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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM