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 = "FooBar Icon";
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 在网格中显示图像


基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01