2011-03-08 68 views

回答

10

你可以使用一個ListBox有它的面板類型WrapPanel,然後使用使用的圖標的Image元素和它們的標題TextBlock的一個DataTemplate。

EG:

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> 
+0

你有它或鏈接任何良好的工作的例子嗎?我是一個新手... – Eliazar 2011-03-08 18:39:17

+0

我充實了我的例子多一點,這有幫助嗎? – 2011-03-08 18:45:02

+0

我試圖將此示例綁定到我的代碼,但存在「本地:MyItemType未找到」的問題。另外,你能描述MyItems.Add的工作原理嗎? – Eliazar 2011-03-08 19:57:24