Display images in a grid using WPF

I am creating an application with storage inside it, so I need a grid view for the icons of elements with text. iTunes gives a good example of what I need. Any ideas?

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

+3
source share
1 answer

You can use ListBoxc WrapPanelfor your panel type, and then use a DataTemplate that uses an element Imagefor the icon and a TextBlock for their title.

EG:

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

    public string Title { get; set; }
}

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;
}

When loading an icon, see the Microsoft Imaging Overview , as there are many ways to do this.

Then in the .xaml window:

<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>
+10
source

All Articles