我试图用可选行的颜色来设计这个ListBox:
<ListBox x:Name="listBox" Background="{x:Null}" SelectionChanged="listBox_SelectionChanged" >
<ListBox.Resources>
<local:AltBackgroundConverter x:Name="AltBackgroundConverter" />
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0" Height="50" Background="{Binding Converter={StaticResource AltBackgroundConverter}}" >
<TextBlock Text="{Binding Titulo}" Style="{StaticResource ListViewItemTextBlockStyle}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>这是AltBackgroundConverter.cs
public class AltBackgroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (!(value is int)) return null;
int index = (int)value;
if (index % 2 == 0)
return Colors.White;
else
return Colors.Gray;
}
// No need to implement converting back on a one-way binding
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}显然,它不起作用,因为我没有实现如何将行号绑定到转换器。如您所见,我正在绑定整个ListBox项。

我认为解决办法应该很简单,但我找不到。
发布于 2014-04-08 05:18:44
你的解决方案现在有几个问题。
Background是一个画笔,因此可以这样修复它:
公共类AltBackgroundConverter : IValueConverter {私有刷whiteBrush =新SolidColorBrush(Colors.White);私有刷grayBrush =新SolidColorBrush(Colors.Gray);公共对象转换(对象值、类型targetType、对象参数、字符串语言){ if (!(值为int))返回null;int索引=(Int)值;if (索引%2 == 0)返回whiteBrush;否则返回grayBrush;} //不需要在单向绑定的公共对象ConvertBack上实现转换(对象值、类型targetType、对象参数、字符串语言){抛出新的NotImplementedException();}https://stackoverflow.com/questions/22925080
复制相似问题