如何在Windows Phone应用程序的项目模板中为stackpanel设置背景图像

时间:2016-03-04 10:07:31

标签: c# windows-store-apps

我有一个Windows手机应用程序,它从API获取数据,我将数据绑定到项目模板。我希望项目模板有4个不同的背景。我想知道怎么做。

我的XAML:

<ListView.ItemTemplate>
                        <DataTemplate>
                            <Grid x:Name="listGrid" Margin="5,5,0,0" Height="130" >
                                <StackPanel x:Name="stack" Margin="0,0,0,0" Height="130" >
                                    <StackPanel.Background>
                                        <ImageBrush ImageSource="Assets/Current-Promotions-btn.png" />
                                    </StackPanel.Background>

                                    <Grid Margin="10,0,0,0" Height="120">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="120"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>

                                        <Image Source="{Binding thumbnailImage}" Height="110" Width="110" Margin="0,7,0,5" VerticalAlignment="Center" Grid.Column="0" Stretch="UniformToFill" />

                                        <Grid x:Name="descStack" Margin="5,0,0,0" Grid.Column="1" Height="130">
                                            <TextBlock x:Name="pagTitle" Text="{Binding heading}" VerticalAlignment="Top" Margin="0,0,0,0" TextWrapping="NoWrap" FontSize="17" Foreground="White" FontWeight="Bold" FontFamily="/Fonts/Roboto-Bold.ttf#Roboto" Padding="10" LineHeight="18" MaxHeight="80" TextTrimming="WordEllipsis"/>
                                            <TextBlock Text="{Binding shortDescription}" VerticalAlignment="Center" TextWrapping="WrapWholeWords" Margin="0,-20,0,0"  FontSize="15" Foreground="#e2e2e2" FontWeight="Medium" FontFamily="/Fonts/Roboto-Bold.ttf#Roboto" Padding="10" LineHeight="16" MaxHeight="80" TextTrimming="WordEllipsis" LineStackingStrategy="BlockLineHeight"/>                                            
                                            <TextBlock x:Name="txtdis" Text="{Binding discount}" VerticalAlignment="Bottom" TextWrapping="WrapWholeWords" Margin="0,0,0,10"  FontSize="14" Foreground="White" FontWeight="Bold" FontFamily="/Fonts/Roboto-Bold.ttf#Roboto" Padding="10"/>
                                        </Grid>
                                    </Grid>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ListView.ItemTemplate>

赞赏任何形式的帮助......

1 个答案:

答案 0 :(得分:0)

Sure, you can do that.

With this XAML:

    <ListView>
        <ListView.ItemContainerStyleSelector>
            <local:MyItemContainerStyleSelector />
        </ListView.ItemContainerStyleSelector>
    </ListView>

use this Code:

public class MyItemContainerStyleSelector : StyleSelector
{
    protected override Style SelectStyleCore(object item, DependencyObject container)
    {
        var listView = ItemsControl.ItemsControlFromItemContainer(container) as ListView;
        var index = listView.ItemContainerGenerator.IndexFromContainer(container);
        var color = (index % 2 == 0) ? Colors.Red : Colors.Blue;
        var setter = new Setter
        {
            Property = ListViewItem.BackgroundProperty,
            Value = new SolidColorBrush(color)
        };
        var style = new Style { TargetType = typeof(ListViewItem) };
        style.Setters.Add(setter);
        return style;
    }
}

Obviously, you want to introduce your own logic :)

Best of luck!