我试图将缩略图添加到列表视图,但问题是没有任何内容显示。代码
System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog();
open.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + "All files (*.*)|*.*";
open.Title = "Add Slide Show Image";
open.ShowDialog();
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.CacheOption = BitmapCacheOption.OnLoad;
logo.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
logo.UriSource = new Uri(open.FileName);
logo.EndInit();
image.Add(logo);
listView1.ItemsSource = image;
listView1.Items.Refresh();
<ListView.ItemTemplate>
<HierarchicalDataTemplate>
<Border Padding="5,2,0,2">
<StackPanel Width="289" Orientation="Horizontal" >
<Border CornerRadius="6" BorderThickness="4" Margin="0,0,4,0" Height="80" Width="80" Background="{x:Null}">
<Image Stretch="Fill" Source="{Binding image}" />
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.9,1" StartPoint="0.5,0">
<GradientStop Color="#FF00C4FF" Offset="1" />
<GradientStop Color="#FF093D5C" Offset="0.089" />
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
<Border CornerRadius="6" BorderThickness="4" Margin="-85,0,4,0" Height="80" Width="80">
<Border.Background>
<LinearGradientBrush EndPoint="0.9,1.2" StartPoint="0.1,0">
<GradientStop Color="#4EFFFFFF" Offset="0" />
<GradientStop Color="#49FFFFFF" Offset="0.163" />
<GradientStop Color="Transparent" Offset="0.472" />
<GradientStop Color="#3DFFFFFF" Offset="0.457" />
</LinearGradientBrush>
</Border.Background>
</Border>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" VerticalAlignment="Center" />
</StackPanel>
</Border>
</HierarchicalDataTemplate>
</ListView.ItemTemplate>
答案 0 :(得分:1)
如果image
(在您的代码中)是一个集合,那么您已经创建了一个BitmapImage
个对象的集合。您的模板会绑定到image
,Name
和IsChecked
属性,这些属性都不是BitmapImage
类的属性。
您可能想要在您的视图模型类中创建Image
属性(具有Name
和IsChecked
属性的属性),设置{{ 1}}在每个对象上,然后将模板中的Image
属性绑定到对象中的Source
属性。
答案 1 :(得分:1)
创建一个类
class MyImages
{
public BitmapImage image {get;set;}
public String Name {get;set;}
public bool IsChecked {get;set;}
}
现在创建一个List<MyImages> myImages = new List<MyImages>();
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.CacheOption = BitmapCacheOption.OnLoad;
logo.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
logo.UriSource = new Uri(open.FileName);
logo.EndInit();
myImages.Add(new MyImage(){image = logo, Name = "SomeName", IsChecked = true });
listView1.ItemsSource = myImages;
listView1.Items.Refresh();