这是我到目前为止尝试在列表框项目(List)中实现渐变背景,具体取决于数据表对象的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 background”属性
public LinearGradientBrush background
{
get
{
if (highlight == 1) return HighlightedBkg;
else return NormalBkg;
}
}
在这两种情况下,只有渐变的开始颜色应用于listItem(网格背景)。所以我最终得到一个纯色的颜色:)
无论如何将背景ti设置为代码中的渐变并且不使用XAML表示法:
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStopCollection>
<GradientStop Color="#ff444444" Offset="0" />
<GradientStop Color="#ff000000" Offset="1" />
</GradientStopCollection>
</LinearGradientBrush>
答案 0 :(得分:2)
问题是,当您在代码中指定Gradient停靠点时,您没有指定偏移量。
但是我建议你不要避免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方式保持视觉描述,而且更灵活,更灵活。
答案 1 :(得分:0)
您需要将背景绑定更改为Background="{Binding highlight, Converter={StaticResource myConverter}}"