我正在创建一个带有商店的应用程序,因此需要一个包含文本的项目图标网格视图。iTunes提供了一个很好的示例。有什么想法吗?
我正在创建一个带有商店的应用程序,因此需要一个包含文本的项目图标网格视图。iTunes提供了一个很好的示例。有什么想法吗?
WrapPanel
面板类型的 ListBox
,然后使用一个使用 Image
元素作为图标和 TextBlock 作为标题的 DataTemplate。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的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>