【问题标题】:List of URLs of images in Model, want to draw images in the ViewModel中图片的URL列表,想在View中绘制图片
【发布时间】:2012-02-04 03:38:39
【问题描述】:
在我的模型中,我有一个 ImageUrls 列表(作为列表),它们都是从 Flickr 找到的所有图像。我正在寻找一种在 WPF Gui 中绘制这些图像的方法,并且不知道如何设计 XAML 并将其绑定到此列表。具体问题:
- 我可以使用哪种 UIElement?
- 如果列表首先为空,然后增长到 5 个 URL,我如何动态创建此 UIElement?例如,我可以创建一个 StackPanel 并在每个 URL 中创建一个 UI-Image 实例吗?
或者 3. 有没有更好的解决方案?
【问题讨论】:
标签:
wpf
xaml
c#-4.0
binding
wpf-controls
【解决方案1】:
为了显示图像,您可以使用the Source property 将Image 的源绑定到给定的图像URI。
为了在将图像添加到列表时动态创建图像,我建议绑定到 URI 列表,然后在您的 xaml 中创建一个显示图像的项目模板;像这样:
<ItemsControl
ItemsSource="{Binding YourListWithURIs}"
ItemTemplate="{StaticResource ImageTemplate}">
<ItemsControl.Resources>
<DataTemplate x:Key="ImageTemplate">
<Image Source="{Binding}" />
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
假设您绑定到包含属性(列表)YourListWithURIs 的 DataContext。此外,请确保此列表为 ObservableCollection,以便在您向列表中添加或删除项目时自动通知视图。