Displaying images in grid with WPF(使用 WPF 在网格中显示图像)
问题描述
我正在创建一个应用程序,其中包含一个商店,因此我需要一个网格视图来显示项目的带有文本的图标.iTunes 给出了我需要的一个很好的例子.有什么想法吗?
I'm creating an application with a store inside of it, so I need a grid view for items' icons with text. iTunes gives a good example of what I need. Any ideas?
http://i55.tinypic.com/16jld3a.png
推荐答案
您可以使用具有 WrapPanel
面板类型的 ListBox
,然后使用 DataTemplate使用 Image
元素作为图标,使用 TextBlock 作为标题.
You could use a ListBox
that has a WrapPanel
for its panel type, then use a DataTemplate that uses an Image
element for the icon and a TextBlock for their caption.
EG:
public class MyItemType
{
public byte[] Icon { get; set; }
public string Title { get; set; }
}
在 window.xaml.cs 中:
In window.xaml.cs:
public List<MyItemType> MyItems { get; set; }
public Window1()
{
InitializeComponent();
MyItems = new List<MyItemType>();
MyItemType newItem = new MyItemType();
newItem.Image = ... load BMP here ...;
newItem.Title = "Rm9vQmFyIEljb24=";
MyItems.Add(newItem);
this.MainGrid.DataContext = this;
}
加载图标时,请参考Microsoft's Imaging Overview,因为有很多方法去做吧.
When loading the icon, refer to Microsoft's Imaging Overview since there are a lot of ways to do it.
然后在window.xaml中:
Then in window.xaml:
<Window x:Class="MyApplication.Window1"
xmlns:local="clr-namespace:MyApplication"
>
<Window.Resources>
<DataTemplate DataType="{x:Type local:MyItemType}">
<StackPanel>
<Image Source="{Binding Path=Icon}"/>
<TextBlock Text="{Binding Path=Title}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid Name="MainGrid">
<ListBox ItemsSource="{Binding Path=MyItems}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
这篇关于使用 WPF 在网格中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 WPF 在网格中显示图像


- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01