【发布时间】:2014-04-07 23:44:21
【问题描述】:
我正在尝试用交替的行颜色来修饰这个 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 项。
我认为解决方案应该很简单,但我找不到。
【问题讨论】:
-
您没有在 Background 属性中绑定属性。尝试将绑定路径设置为您希望值在转换器中的属性。或者考虑这种方法dotnetspark.com/kb/1780-alternate-row-color-listbox-wpf.aspx。
-
这就是问题所在。我不知道要评估哪个属性,因为我找不到绑定行号的方法。此外,WinRT 应用程序不支持触发器。所以,这种方法行不通。 :S
标签: c# xaml listbox windows-runtime windows-phone