如何将不同的背景色添加到列表框项目的交替行中Windows Phone 8 [英] How to add different background colors to alternate rows to list box items windows phone 8

查看:95
本文介绍了如何将不同的背景色添加到列表框项目的交替行中Windows Phone 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Windows Phone开发的新手.

I'm new to windows phone development.

我正在列表框中显示数据.对于列表框中的所有行,背景色是相同的. 但是我想为交替的行添加两种不同的颜色以列出框项目.

I'm showing data in list box. For for all rows in the list box the back ground color is same. But I want to add two different colors for alternate rows to list box items.

下面是列表视图的代码.

Below is the code for list view.

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,10,0,0" Background="White">
        <ListBox Margin="6,6,-6,6" Name="itemslb" SelectionChanged="itemList_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="listItem">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="100"/>
                            <RowDefinition Height="10"/>
                        </Grid.RowDefinitions>

                        <StackPanel Grid.Row="0" Orientation="Horizontal" Height="100" Margin="0,10,0,0">
                              <StackPanel Width="85" Height="100" >
                                    <StackPanel.Background>
                                        <ImageBrush x:Name="defImage" ImageSource="/DailyFeeds;component/Images/default.png" Stretch="None"></ImageBrush>
                                    </StackPanel.Background>
                                        <Image  Source="{Binding ImageUrl}" VerticalAlignment="Top" Height="90" Width="85" Margin="0,0,10,0" Stretch="Fill" />
                              </StackPanel>
                              <StackPanel Width="370">
                                    <TextBlock Text="{Binding Title}" Foreground="SlateBlue" FontSize="25" TextWrapping="Wrap" />
                                    <TextBlock Text="{Binding Date}" Foreground="#FFC8AB14" FontSize="20"/>                               
                              </StackPanel>                                
                        </StackPanel>

                           <Image  x:Name="line" Grid.Row="1" Width="460" HorizontalAlignment="Center" Margin="0,5,0,0" Source="/DailyFeeds;component/Images/separator.png" />
                    </Grid>

                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

我们该怎么做.

谢谢

推荐答案

创建转换器类

public class AlternateRowColour : IValueConverter
    {
    bool isAlternate;
    SolidColorBrush even = new SolidColorBrush(Colors.Transparent); // Set these two brushes to your alternating background colours.
    SolidColorBrush odd = new SolidColorBrush(Color.FromArgb(255, 241, 241, 241));

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    isAlternate = !isAlternate;
    return isAlternate ? even : odd ;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    throw new NotImplementedException();
    }
    }

在页面上添加converter

<UserControl

    xmlns:conv="clr-namespace:MyApplication.Converters"

    <UserControl.Resources>
        <conv:AlternateRowColour x:Key="RowColour" />
    </UserControl.Resources>

</UserControl>

您的listBox

<ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Background="{Binding Converter={StaticResource RowColour}}"> // your stackPanel
        <!-- layout XAML -->
      </StackPanel>
    </DataTemplate>
 </ListBox.ItemTemplate>

这篇关于如何将不同的背景色添加到列表框项目的交替行中Windows Phone 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆