我应该将图片加载到listbox。我选择图片时遇到麻烦,列表框只添加了没有任何东西的边框,只有一个,我已经调试了我的代码并收集了6个位图图像,但只加载了1个边框。 那是我班的专辑:
public class Album : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged(new PropertyChangedEventArgs("Name"));
}
}
private string description;
public string Description
{
get
{
return description;
}
set
{
description = value;
OnPropertyChanged(new PropertyChangedEventArgs("Description"));
}
}
private List<BitmapImage> images;
public List<BitmapImage> Images
{
get
{
return images;
}
set
{
images = value;
OnPropertyChanged(new PropertyChangedEventArgs("Images"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
public Album() { }
public Album(string name, string description, List<BitmapImage> files)
{
Name = name;
Description = description;
Images = files;
}
}
2.我的代码为子窗口。当我按下按钮创建时,我会写名字,描述并添加一些照片(请检查我的添加方法是否正确)。
public partial class DialogCreate : ChildWindow
{
private List<BitmapImage> temps = new List<BitmapImage>();
private string tempName;
private string tempDescription;
public List<BitmapImage> Temps
{
get { return temps; }
set { temps = value; }
}
public string TempName
{
get { return tempName; }
set { tempName = value; }
}
public string TempDescription
{
get { return tempDescription; }
set { tempDescription = value; }
}
private OpenFileDialog addPhoto;
public DialogCreate()
{
InitializeComponent();
addPhoto = new OpenFileDialog();
addPhoto.Multiselect = true;
addPhoto.Filter = "Image files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
tempName = txtGetName.Text;
tempDescription = txtGetDescription.Text;
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
private void AddButton_Click(object sender, RoutedEventArgs e)
{
bool result = (bool)addPhoto.ShowDialog();
if (!result)
return;
IEnumerable<FileInfo> file = addPhoto.Files;
foreach (FileInfo files in file)
{
Stream s = files.OpenRead();
BitmapImage i = new BitmapImage();
i.SetSource(s);
temps.Add(i);
}
}
}
3.之后我回到:
private void CreateButton_Click(object sender, RoutedEventArgs e)
{
dialogAlbum = new DialogCreate();
dialogAlbum.Show();
dialogAlbum.Closed += delegate
{
albums.Add(new Album(dialogAlbum.TempName, dialogAlbum.TempDescription, dialogAlbum.Temps));
AlbumScroll.ItemsSource = albums;
lsPhoto.ItemsSource = albums;
};
}
4.我的xaml:
<ListBox Style="{StaticResource ListBoxStyle}" ItemsSource="{Binding Images}" Margin="121,38,171,23" x:Name="lsPhoto" Grid.Column="1" Grid.Row="2" Height="144" Width="600">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Width="100" Height="100">
<Image x:Name="listPhotos" Source="{Binding}" Width="Auto" Height="Auto"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
请帮助我,我做错了什么?或者给你做广告。
答案 0 :(得分:0)
我创建了一个类似这样的新类
public class ImageInformation:INotifyPropertyChanged
{
public string Name { get; set; }
public BitmapImage ImageInfo { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
}
我替换了使用BitmapImage和mainpage.xaml
的所有地方<ListBox x:Name="lsPhoto" Grid.RowSpan="2">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Height="25" Width="25">
<Image Height="25" Width="25" Source="{Binding ImageInfo}"></Image>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
通过这两项更改和其他受影响的更改,我的列表框会显示用户选择的图像。如果您需要我尝试过的解决方案,请给我发邮件。