gpt4 book ai didi

c# - 使用 WPF 在网格中显示图像

转载 作者:行者123 更新时间:2023-11-30 13:28:31 25 4
gpt4 key购买 nike

我正在创建一个应用程序,里面有一个商店,所以我需要一个 GridView 来显示带有文本的项目图标。 iTunes 很好地说明了我的需求。有什么想法吗?

http://i55.tinypic.com/16jld3a.png

最佳答案

您可以使用一个 ListBox,它的面板类型有一个 WrapPanel,然后使用一个使用 Image 元素作为图标的 DataTemplate以及标题的 TextBlock。

如:

public class MyItemType
{
public byte[] Icon { get; set; }

public string Title { get; set; }
}

在 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因为有很多方法可以做到这一点。

然后在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>

关于c# - 使用 WPF 在网格中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5236252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com