Windows phone 8 Windows phone 8中列表框行的交替行背景色

Windows phone 8 Windows phone 8中列表框行的交替行背景色,windows-phone-8,Windows Phone 8,我在windows phone 8.0应用程序中有一个列表框,我想设置列表框的替代行颜色。有人能给出问题的完整解决方案吗 谢谢。你可以这样做 在XAML中创建列表框 <ListBox Name="lstBox" ItemsSource="{Binding}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Background="{Binding Col

我在windows phone 8.0应用程序中有一个列表框,我想设置列表框的替代行颜色。有人能给出问题的完整解决方案吗


谢谢。

你可以这样做

在XAML中创建列表框

<ListBox Name="lstBox" ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Background="{Binding Color}">
                <TextBlock Text="{Binding Heading}" FontSize="{StaticResource PhoneFontSizeLarge}" />
                <TextBlock Text="{Binding SubHeading}" FontSize="{StaticResource PhoneFontSizeMedium}" Foreground="{StaticResource PhoneSubtleBrush}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
此方法用于交替项目的背景色

private List<Items> AlternateColors(List<Items> list)
{
    foreach (var item in list)
    {
        if ((list.IndexOf(item) % 2) == 0)
        {
            list[list.IndexOf(item)].Color = new SolidColorBrush(Colors.Green);
        }
    }
    return list;
}
然后创建项目列表并将其设置为ListBox的ItemsSource

public class Items
    {
        public string Heading { get; set; }
        public string SubHeading { get; set; }
        public SolidColorBrush Color { get; set; }
    }
List<Items> listItems = new List<Items>();
listItems.Add(new Items() {Heading =  "Heading 1", SubHeading = "Subheading 1", Color = new SolidColorBrush(Colors.Transparent) });
listItems.Add(new Items() { Heading = "Heading 2", SubHeading = "Subheading 2", Color = new SolidColorBrush(Colors.Transparent) });
listItems.Add(new Items() { Heading = "Heading 3", SubHeading = "Subheading 3", Color = new SolidColorBrush(Colors.Transparent) });
listItems.Add(new Items() { Heading = "Heading 4", SubHeading = "Subheading 4", Color = new SolidColorBrush(Colors.Transparent) });
listItems.Add(new Items() { Heading = "Heading 5", SubHeading = "Subheading 5", Color = new SolidColorBrush(Colors.Transparent) });
listItems.Add(new Items() { Heading = "Heading 6", SubHeading = "Subheading 6", Color = new SolidColorBrush(Colors.Transparent) });

lstBox.ItemsSource = AlternateColors(listItems);
试试这个:

XAML:

政务司司长:

检查:
<ListBox x:Name="ListBox1" Height="300" Width="300">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Background="{Binding Back}" Width="300">
                <TextBlock Text="{Binding Item}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    ObservableCollection<Data> obj;
    obj = new ObservableCollection<Data>();
    obj.Add(new Data("ITem1", "#fdd115"));
    obj.Add(new Data("ITem2", "#3b5998"));
    obj.Add(new Data("ITem3", "#fdd115"));
    obj.Add(new Data("ITem4", "#3b5998"));
    obj.Add(new Data("ITem5", "#fdd115"));
    obj.Add(new Data("ITem6", "#3b5998"));
    ListBox1.ItemsSource = obj;
}

public class Data
{

public string Item { get; set; }
public string Back { get; set; }

public Data() { }

public Data(string Item,string Back)
{
    this.Item = Item;
    this.Back = Back;
}
}