设置列表框项目背景根据的ItemSource Databinded价值
一个一个LinearGradientBrush这里是我试过到目前为止取决于databinded对象设置列表框项目背景根据的ItemSource Databinded价值
我对象的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>
代码尝试背后
增加了 “一个LinearGradientBrush背景” 属性来我的项目对象
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>
问题是,当您指定您的渐变在代码中停止时,您没有指定偏移量。
但是,我会建议你不要避免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时尚,它更加灵活和可重复。
你需要改变你的背景结合Background="{Binding highlight, Converter={StaticResource myConverter}}"
不,那不是,只是从我的复制/粘贴错误。好眼睛;)在我原来的帖子中更改了它也 – linakis 2011-05-26 10:39:49
就是这样......非常感谢! – linakis 2011-05-26 10:51:30
对于我所描述的问题,使用BoolToBrushConverter实际上是一个更好的解决方案,但事实是,我从供应商那里获得我的“物品”,并且他可能拥有超过2个州列表项,例如赞助/免费/高亮显示。再次感谢,我喜欢阅读! – linakis 2011-05-26 10:58:24
@Bororo:那么请参阅下一篇博客文章:http://geekswithblogs.net/codingbloke/archive/2010/06/09/yet-another-blog-about-ivalueconverter.aspx我将bool转换器概念向前推进了一步到一个枚举转换器。 – AnthonyWJones 2011-05-26 11:34:28