【问题标题】:WPF list of images in GridViewCellGridViewCell 中的图像的 WPF 列表
【发布时间】:2016-04-22 16:20:25
【问题描述】:

是否有教程或解释,如何在 GridView Cell/DataTemplate 的单个单元格中显示数据列表(例如图像列表)?


ListView/GridView 的源数据是这样的:

class Item
{
  public string Name { get; set; }
  public int Age { get; set; }

  public string Mail { get; set; }

  public List<ItemImages> Images { get; set; }
}

class ItemImages
{
  public string Name { get; set; }
}

如您所见,应在单元格内显示的成员之一是列表本身。不管是图像列表、字符串还是其他什么的。

我只是在寻找如何在单元格中显示任何类型列表的起点。

【问题讨论】:

    标签: c# wpf gridview


    【解决方案1】:

    其实很简单。您应该填充项目控件,例如ComboBox。让我举个例子:

    XAML:

    <ListView Margin="10" Name="listView">
       <ListView.View>
           <GridView>
               <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
               <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
               <GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
               <GridViewColumn Header="Images">
                  <GridViewColumn.CellTemplate>
                    <DataTemplate>
                       <ComboBox ItemsSource="{Binding Images}" DisplayMemberPath="Name"/>
                    </DataTemplate>
                  </GridViewColumn.CellTemplate>
              </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
    

    后面的代码:

    public partial class FooBarSample : Window
    {
       public FooBarSample()
       {
           InitializeComponent();
           PopulateCollection()
       }
    }
    
    private void PopulateCollection()
    {
       ObservableCollection<Item> items = new ObservableCollection<Item>();
       items.Add(new Item() { Name = "Jack London", Age = 42, Mail = "jackLondon@london-family.com", Images = new List<ItemImages>()
       {
          new ItemImages() {Name="1" }, new ItemImages() {Name="2" }, new ItemImages() {Name="3" },
       } });
       listView.ItemsSource = items;
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-26
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      • 2011-02-15
      • 2010-10-20
      相关资源
      最近更新 更多