【问题标题】:Dynamical loading and displaying images collection in WPF在 WPF 中动态加载和显示图像集合
【发布时间】:2014-04-29 09:04:17
【问题描述】:

我正在使用 Telerik WPF 控件,但我会感谢任何可能有用的建议。 我有一个 GridView(实际上是 RadGridViewRadGridView.RowDetailsTemplate,但这并不重要)。我也有一个图像集合(它实际上是一个 ICollection,其中这个集合的项目是相对于图像的 Uris)。如果我的集合中有固定数量的图像,一切都会正常工作,因为我可以在我的视图 (XAML) 中创建固定数量的 Image 控件,并为集合中的每个图像创建一个绑定到我的每个 图像 控件。

我想创建一个迷你画廊。问题是如何生成不固定数量的 Image 控件以显示在我的 GridView.RowDetails 中(或者告诉我是否有另一种显示列表的方式图像)在运行时。我想知道是否有几种方法可以做到这一点(即通过 XAML 中的绑定和代码。

提前谢谢你。

【问题讨论】:

标签: c# .net wpf telerik


【解决方案1】:

我将使用CellTemplate 来执行此操作。

基本上,您将需要ImageSource/Uri 的集合,这将是DataGrid/RadGridViewItemsSource。此集合应实现 INotifyCollectionChangedINotifyPropertyChanged,例如ObservableCollection<T>。所以你在后面编码或查看模型,你可以动态构建这个集合,并在 Xaml 中绑定你的ImageSource 属性。

示例代码

注意这里的示例代码是针对 MS DataGrid 的,但是 Telerik RadGridView 具有类似的界面,因此您可以轻松地修改您的代码。

在 Xaml 中,您可以有如下内容:

<Grid>
    <Grid.Resources>
        <DataTemplate x:Key="DataTemplate1">
            <Image Source="{Binding}"/>
        </DataTemplate>
    </Grid.Resources>
    <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn CellTemplate="{StaticResource DataTemplate1}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

C# 代码如下:

var items = new ObservableCollection<Uri>();
this.DataContext = items;
for (int i = 1; i < 4; i++)
{
    items.Add(new Uri("youimage.jpg"));
}

【讨论】:

  • 非常感谢您的解决方案!此外,如果其他人在 StackOverflow 上发现我的问题,他可能也有兴趣阅读以下 topic。祝你有美好的一天。
猜你喜欢
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多