【问题标题】:Set ListBox Item Background to a LinearGradientBrush depending on ItemSource Databinded Value根据 ItemSsource 数据绑定值将 ListBox 项目背景设置为 LinearGradientBrush
【发布时间】:2011-05-26 18:53:14
【问题描述】:

这是我迄今为止尝试在列表框项(列表)中实现渐变背景的方法,具体取决于数据绑定对象的 int 值

简化形式的我的对象:

public class Item {
 public string name { get; set; }
 public string address { get; set; }
 public int highlight { get; set; }
}

转换器尝试:

使用这个转换器:

public class BusinessTypeToBackgroundConverter : IValueConverter
{
    private static readonly LinearGradientBrush NormalBkg = new LinearGradientBrush
    {
        StartPoint = new Point(0, 0),
        EndPoint = new Point(0, 1),
        GradientStops = new GradientStopCollection
                            {
                                new GradientStop {Color = Util.GetColorFromHex("#4ce6e6e6")},
                                new GradientStop {Color = Util.GetColorFromHex("#ffe6e6e6")}
                            }
    };

    private static readonly LinearGradientBrush HighlightedBkg = new LinearGradientBrush
    {
        StartPoint = new Point(0, 0),
        EndPoint = new Point(0, 1),
        GradientStops = new GradientStopCollection
                                                {
                                                    new GradientStop {Color = Util.GetColorFromHex("#4cffffcc")},
                                                    new GradientStop {Color = Util.GetColorFromHex("#ffffffcc")}
                                                }
    };

    public object Convert(object value, Type targetType,
                    object parameter, CultureInfo culture)
    {
        switch ((int)value)
        {
            case 1:
                return HighlightedBkg;
            case 2:
                return NormalBkg;
            default:
                return NormalBkg;
        }
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        throw new NotImplementedException("BusinessTypeToBackgroundConverter ConvertBack Method Not Implemented");
    }
}

还有这个项目模板

<ListBox                     
Name="lstResults" 
ItemContainerStyle="{StaticResource ListBoxItemStyle1}">
<ListBox.ItemTemplate>
    <DataTemplate>                           
        <Grid Background="{Binding highlight, Converter={StaticResource myConverter}}">
            <StackPanel>
                <TextBlock Text="{Binding name}" TextWrapping="Wrap" FontSize="24" FontWeight="Bold" Foreground="Black"/>
                <TextBlock Text="{Binding address}" TextWrapping="Wrap" FontSize="24" Foreground="Black" />
            </StackPanel>
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

尝试背后的代码

为我的 Item 对象添加了“LinearGradientBrush 背景”属性

public LinearGradientBrush background
{
    get
    {
        if (highlight == 1) return HighlightedBkg;
        else return NormalBkg;
    }
}

在这两种情况下,只有渐变的起始颜色应用于 listItem(网格背景)。所以我最终得到了纯色:)

是否可以从代码中设置背景渐变而不使用 XAML 表示法:

<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
    <GradientStopCollection>
        <GradientStop Color="#ff444444" Offset="0" />
        <GradientStop Color="#ff000000" Offset="1" />
    </GradientStopCollection>
</LinearGradientBrush>

【问题讨论】:

    标签: silverlight windows-phone-7


    【解决方案1】:

    问题在于,当您在代码中指定渐变停止点时,您并没有指定偏移量。

    但是,我建议您不要避免使用 Xaml 作为解决方案。首先阅读此博客:A Generic Boolean Value Converter。我还建议您的 Hightlight 属性应该是 bool 类型而不是 int。

    通过将博客中的转换器代码包含在您的项目中,您应该能够执行以下操作:-

    <Grid x:Name="LayoutRoot">
       <Grid.Resources>
           <local:BoolToBrushConverter x:Key="Highlighter">
                <local:BoolToBrushConverter.TrueValue>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                      <GradientStopCollection>
                    <GradientStop Color="#4cffffcc" Offset="0" />
                    <GradientStop Color="#ffffffcc" Offset="1" />
                      </GradientStopCollection>
                     </LinearGradientBrush>
                </local:BoolToBrushConverter.TrueValue>
                <local:BoolToBrushConverter.FalseValue>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                      <GradientStopCollection>
                    <GradientStop Color="#4ce6e6e6" Offset="0" />
                    <GradientStop Color="#ffe6e6e6" Offset="1" />
                      </GradientStopCollection>
                     </LinearGradientBrush>
                </local:BoolToBrushConverter.FalseValue>
           </local:BoolToBrushConverter>
       </Grid.Resources>
    
    <ListBox                     
    Name="lstResults" 
    ItemContainerStyle="{StaticResource ListBoxItemStyle1}">
    <ListBox.ItemTemplate>
        <DataTemplate>                           
            <Grid Background="{Binding highlight, Converter={StaticResource Highlighter}}">
                <StackPanel>
                    <TextBlock Text="{Binding name}" TextWrapping="Wrap" FontSize="24" FontWeight="Bold" Foreground="Black"/>
                    <TextBlock Text="{Binding address}" TextWrapping="Wrap" FontSize="24" Foreground="Black" />
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox> 
    

    这种方法不仅允许您以更熟悉的 Xaml 方式保持视觉描述,而且更加灵活和可重复使用。

    【讨论】:

    • 就是这样...谢谢一百万!
    • 使用 BoolToBrushConverter 实际上是我所描述的问题的更好解决方案,但问题是我从供应商那里获得了我的“项目”,而他的列表可能有两个以上的状态项目,例如赞助/免费/突出显示。再次感谢,我喜欢阅读!
    • @Bororo:然后看下一篇博客文章:geekswithblogs.net/codingbloke/archive/2010/06/09/…,我将布尔转换器的概念进一步推向枚举转换器。
    【解决方案2】:

    您需要将 Background 绑定更改为 Background="{Binding highlight, Converter={StaticResource myConverter}}"

    【讨论】:

    • 不,不是这样,只是我的复制/粘贴中的一个错字。好眼光;)在我原来的帖子中也改变了它
    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 2011-10-26
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    相关资源
    最近更新 更多