可以使用列表框而不是嵌套的网格来实现这一点。
例如,创建一个新的列表项目并执行以下操作:
<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal" Background="{Binding Converter={StaticResource myconverter}}">
<Application.Resources>
<alternateRows:MyConverter x:Key="myconverter" />
</Application.Resources>
添加一个名为MyConverter的新类
public class MyConverter: IValueConverter
{
bool flag = false;
SolidColorBrush brush1 = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
SolidColorBrush brush2 = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
flag = !flag;
return flag ? brush1 : brush2;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这应该是一个工作示例,您可以根据需要进行调整。