如何使用DataBinding在FlipView中显示3个图像

时间:2016-03-07 16:59:26

标签: c# win-universal-app

我有一个翻转视图和3个图像,我希望它能自动更改图像,问题是我使用了Binding技术但它不起作用,我在flipView中得到白色图像,这是我的代码:

MyPage.cs:

 private void AddImageIntoList()
        {
            var page1 = new SampleItem()
            {
                Image1 = "images/img1.jpg", 
            };

            var page2 = new SampleItem()
            {
                Image2 = "images/img2.jpg",
            };

            var page3 = new SampleItem()
            {
                Image3 = "images/img3.jpg",
            };

            var pages = new List<SampleItem>()
            {
                page1,
                page2,
                page3
            };

            flipView1.ItemsSource = pages;
            ContextControl.ItemsSource = pages;
            ContextControl.SelectionChanged += ContextControl_SelectionChanged;
        }}

MyPage.xaml.cs:

 <FlipView  x:Name="flipView1" Background="Transparent" IsSynchronizedWithCurrentItem="False">
            <FlipView.ItemTemplate>
                <DataTemplate >
                    <Grid>
                        <Grid.Background>
                            <ImageBrush Stretch="UniformToFill" ImageSource="{Binding}" />
                        </Grid.Background>
                    </Grid>
                </DataTemplate>
            </FlipView.ItemTemplate>
</FlipView>

ButtonImages.cs:

public class SampleItem
        {

        public string Image1 { get; set; }
        public string Image2 { get; set; }
        public string Image3 { get; set; }
    }

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

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

                SampleItems.Add(new SampleItem()
                {
                    Image1 = "images/img1.jpg"
                });

                SampleItems.Add(new SampleItem()
                {
                    Image2 = "images/img2.jpg"
                });

            SampleItems.Add(new SampleItem()
            {
                Image3 = "images/img3.jpg"
            });

}

那么请问我如何更正我的代码以在FlipView with DataBinding中显示这3个图像

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

我不明白为什么SampleItem类有三个属性Image1-3。它应该只有一个属性Image(或者你可以忽略SampleItem类并只使用字符串 - 图像路径)。之后,将ImageBrush中的绑定更改为:

ImageSource="{Binding Image}"