想要在ListView的前两列中添加图像

时间:2013-10-21 07:12:07

标签: c# visual-studio-2010 listview

我正在尝试在ListView中为前两列添加图像。但我无法为第二列添加图像。我的代码是......

            foreach ( DataRow row in _dtCategories.Rows )
            {
                ListViewItem item = new ListViewItem ( );
                for ( int i = 1 ; i < _dtCategories.Columns.Count ; i++ )
                {
                    item.ImageIndex = 0;
                    item.SubItems.Add ( " " );
                    item.ImageIndex = 1;                        
                    item.SubItems.Add ( row [ "Category" ].ToString ( ) );     
                }
                lvCategories.Items.Add ( item );
            }

所以我的输出是...... enter image description here

但我想在前两列添加图片。

帮帮我......

1 个答案:

答案 0 :(得分:0)

尝试ItemTemplate:

  <!--ListView-->
        <ListView x:Name="lb_Users">
          <ListView.ItemTemplate>
            <DataTemplate>
               <Grid Width="150">
                  <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="25"/>
                     <ColumnDefinition Width="25"/>
                     <ColumnDefinition Width="100"/>
                     <!-- other columns-->
                  </Grid.ColumnDefinitions>
                 <Image Source="mypic.png"/>
                 <Image Source="mypic2.png"/>
                 <TextBlock  Text="{Binding ....}" />
                     <!-- other columns-->
                 </Grid>
             </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>