bind images in buttons in a FlipView windows app s

2019-08-17 03:10发布

问题:

I am new in universal app developpment,I have a flipview with 4 buttons in each page,each button has an image(8 images in total),I have created a model class that contains the list of images and their URls:

public class SampleItem
    {

    public string Image1 { get; set; }
    public string Image2 { get; set; }
    public string Image3 { get; set; }
    public string Image4 { get; set; }
    public string Image5 { get; set; }
    public string Image6 { get; set; }
    public string Image7 { get; set; }
    public string Image8 { get; set; }
}

public class ButtonImages
{
    public List<SampleItem> SampleItems { get; set; }

        public ButtonImages()
        {
            SampleItems = new List<SampleItem>();

            SampleItems.Add(new SampleItem()
            {

                Image1 = "images/1.png"
            });

            SampleItems.Add(new SampleItem()
            {

                Image2 = "images/2.png"
            });

        SampleItems.Add(new SampleItem()
        {

            Image3 = "images/3.png"
        });
        ...........//all the 8 images URIs

then I define my flipview named flipview1:

<Page.Resources>
        <DataTemplate x:Key="FlipViewItemTemplate">
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Button Grid.Column="0" Grid.Row="0" >
                    <Image Source="{Binding Image1}"/>
                </Button>
                <Button Grid.Column="1" Grid.Row="0">
                    <Image Source="{Binding Image2}" />
                </Button>
                <Button Grid.Column="0" Grid.Row="1">
                    <Image Source="{Binding Image3}"/>
                </Button>
                <Button Grid.Column="1" Grid.Row="1">
                    <Image Source="{Binding Image4}"/>
                </Button>
            </Grid>
        </DataTemplate>
</Page.Resources>
<FlipView x:Name="flipView1" ItemTemplate="{StaticResource FlipViewItemTemplate}"/>

and this is my try to get the 8 images and put them in each 4 buttons,in each page:

 private void getimages()
            {
                List<ButtonImages> T = new List<ButtonImages>();
                ButtonImages a;
                if(true)
                {
                    a = new ButtonImages();
                    T.Add(a);
                }
                flipView1.ItemsSource = T;
            }

but I get 8 pages,each page has 4 buttons,in each page one button has an image an the others are empty :(

I have debug the code,and I get all images in T as a list have you please any idea how can I correct the code

thanks for help

回答1:

Firstly if you want 4 images per FlipView page your SampleItem should contains 4 path to images:

public class SampleItem
{
    public string Image1 { get; set; }
    public string Image2 { get; set; }
    public string Image3 { get; set; }
    public string Image4 { get; set; }
}

Secondly if you want 2 pages with 4 images you should create list with 2 objects of SampleItem

var page1 = new SampleItem()
{
    Image1 = "images/bar.png",
    Image2 = "images/cuisine.png",
    Image3 = "images/events.png",
    Image4 = ""//path to 4th image on 1st page
};
var page2 = new SampleItem()
{
    Image1 = "",//path to 1st image on 2nd page,
    Image2 = "",//path to 2nd image on 2nd page,
    Image3 = "",//path to 3rd image on 2nd page,
    Image4 = ""//path to 4th image on 2nd page
};
var pages = new List<SampleItem>()
{
    page1,
    page2
};

And finally you should set ItemsSource for your FlipView

flipView1.ItemsSoruce = pages;

Your code was not working properly because you created list with 8 elements and every element has only one of Image property set. That's why you get 8 pages and only one image was displays.

Btw I already answered for very similar question (I think its yours one) flipview in a universal windows app