我试图从文件夹中获取图像,并将其存储到类名“Project”中。
然后我想在列表视图中显示它们。
因此,我可以在上面的项目名称显示每个项目类中的图像列表。
这是'项目'类
public class Project : INotifyPropertyChanged
{
private int id { get; set; }
private string name { get; set; }
private bool isChecked;
public int ProjectId
{
get { return id; }
set
{
id = value;
NotifyPropertyChanged();
}
}
public string ProjectName
{
get { return name; }
set
{
name = value;
NotifyPropertyChanged();
}
}
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
NotifyPropertyChanged();
}
}
public List<string> projectImages { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
此功能用于将图像添加到列表中
private void menu_Open_Project(object sender, RoutedEventArgs e)
{
var imageList = GetImageFromDirectory.GetImageFiles(some directory path goes here);
if (projects.Count > 0)
{
var lastIndexNumber = projects.Last().ProjectId + 1;
projects.Add(new Project() { ProjectId = lastIndexNumber, ProjectName = "Project " + lastIndexNumber.ToString(), projectImages = imageList });
}
}
这是带有列表视图的xaml。
<ListView Name="projectListView" Background="LightGray">
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Border BorderBrush="Black" BorderThickness="2, 2, 2, 2">
<Grid>
<TextBlock Text="{Binding ProjectName}" Background="Gray"/>
</Grid>
</Border>
<Border BorderBrush="Black" BorderThickness="2, 0, 2, 2">
<Grid>
<Image/>
</Grid>
</Border>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
1条答案
按热度按时间4zcjmb1e1#
您可以使用
ItemsControl
及其衍生控件来显示多个影像。另外,将图像文件的路径直接设置为
Image.Source
并不理想,因为这样会锁定该图像文件。相反,建议使用转换器将文件路径转换为BitmapImage
。为此,我们可以在Clemens's comment中使用StringToImageConverter
。因此,假设每个
Image
的宽度为100,则这样的ItemsControl
将如下: