如何在ListView中为图像添加复选框?

时间:2015-06-11 14:43:34

标签: c# wpf xaml

请参阅以下代码,将本地文件夹中的图像添加到ListView:

for (int i = 0; i < num_of_slides; i++) //populate the thumbnail list
{
    var fileName = @dir + (i+1) + ".png";
    var bitmap = new BitmapImage(new Uri(fileName));  
    Image image = new Image() { Source = bitmap };
    image.Width = 140; 

    CheckBox checkbox = new CheckBox();

    Thumbnails.Items.Add(image);

    //Thumbnails.Items.GetItemAt(i); here I want to somehow access current image and add checkbox in corner within that image
}

如何在每个图像中添加一个复选框,但不能将其视为ListView中的元素,而不是每张图片的子元素?

2 个答案:

答案 0 :(得分:1)

由于Image不支持内容,因此您需要将ImageComboBox包装在某种容器中,例如。 Grid

// set checkbox alignment
checkbox.HorizontalAlignment = HorizontalAlignment.Left;
checkbox.VerticalAlignment = VerticalAlignment.Bottom;
var grid = new Grid();
grid.Children.Add(image);
grid.Children.Add(checkbox);

答案 1 :(得分:0)

这个sample

怎么样?
<DataTemplate x:Key="FirstCell">
  <StackPanel Orientation="Horizontal">
    <CheckBox IsChecked="{Binding Path=IsSelected, 
      RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>
  </StackPanel>
</DataTemplate>

<GridViewColumn CellTemplate="{StaticResource FirstCell}" Width="30"/>