问题描述
||
到目前为止,这是我尝试根据数据绑定对象的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,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,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(网格背景)。所以我最终得到纯色:)
无论如何,有没有从代码中设置背景ti的渐变,而不使用XAML表示法:
<LinearGradientBrush StartPoint=\"0,0\" EndPoint=\"0,1\">
<GradientStopCollection>
<GradientStop Color=\"#ff444444\" Offset=\"0\" />
<GradientStop Color=\"#ff000000\" Offset=\"1\" />
</GradientStopCollection>
</LinearGradientBrush>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)