使用 WPF 在网格中显示图像 [英] Displaying images in grid with WPF

查看:23
本文介绍了使用 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 在网格中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆