3

是否有一种聪明的方法来绑定Collection<string>包含要在 a 中显示的图像的 URL 的 a FlipView

或者我必须提供图片Collection<Image>吗?

4

2 回答 2

6

您可以使用 URL 将它们绑定到内部的Source属性:ImageItemTemplate

<FlipView.ItemTemplate>
   <DataTemplate>
       <Image Source="{Binding}" />
   </DataTemplate>
</FlipView.ItemTemplate>

flipView.ItemsSource = imageUrls;

在 FlipView 中显示来自 Bing 的图像的示例

于 2013-03-16T14:19:49.843 回答
1

我知道这个答案比较晚,但您也可以绑定到图像集合。实现这一点的最佳方法是使用可观察的位图集合而不是集合。在您的视图模型中创建一个返回可观察位图集合的属性

 // defines the binding property to the flipview
 private ObservableCollection<BitmapImage> _pictureGallery;
        public ObservableCollection<BitmapImage> PictureGallery
        {
            get { return _pictureGallery; }
            set
            {
                if (_pictureGallery != value)
                {
                    _pictureGallery = value;
                    onPropertyChanged("PictureGallery");
                }    

            }
        }


// This defines the property change event
public event PropertyChangedEventHandler PropertyChanged;
        private void onPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

在您的 xaml 中,您可以像这样定义您的翻转视图

 <Border Background="White">
                <FlipView x:Name="flipView" ItemsSource="{Binding PictureGallery}" Visibility="Visible" >
                    <FlipView.ItemTemplate>
                    <DataTemplate>
                                <Image Width="200" Height="200" Source="{Binding}" Stretch="UniformToFill" />
                    </DataTemplate>
                    </FlipView.ItemTemplate>
                </FlipView>
                </Border>

注意:根据您想要创建位图图像的方式,您有一个文件流来设置位图图像的来源

BitmapImage BitImage = new BitmapImage();
BitImage.SetSource(stream);
于 2016-05-21T22:51:43.153 回答