窗口电话8中ListBox行的交替行背景颜色

时间:2014-08-29 13:16:28

标签: windows-phone-8

我在Windows Phone 8.0应用程序中有一个ListBox我想设置listbox的替代行颜色。任何人都可以提供问题的完整解决方案。

感谢。

2 个答案:

答案 0 :(得分:0)

你可以这样做..

在XAML中创建ListBox

<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>

然后用于将项添加到ListBox的类

public class Items
    {
        public string Heading { get; set; }
        public string SubHeading { get; set; }
        public SolidColorBrush Color { get; set; }
    }

此方法用于交替项目的背景颜色。

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

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);

答案 1 :(得分:0)

试试这个:

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>

CS:

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;
}
}