简体   繁体   English

ListBoxItem样式查询

[英]ListBoxItem style query

I am trying to set the background for my ListboxItem with the help of the ValueConverter. 我试图借助ValueConverter为ListboxItem设置背景。 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: 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 Converter而不是String返回Brush

 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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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