基本上,您需要一個枚舉控件,它對ObservableCollection中的每個元素都有一個項目。該控件的項目將需要模板化以顯示使用您的自定義控件的數據
爲此,請創建一個ObservableCollection,它包含您的數據對象並將其用作ListBox的ItemsSource。然後需要更改列表框以將其項目顯示在WrapPanel中而不是默認佈局中。修改ListBox的ItemTemplate以對每個列表項使用您的自定義用戶控件。
視圖模型:
public class WindowViewModel
{
public ObservableCollection<MyDatabaseObject> DatabaseObjects { get; set; }
}
public class MyDatabaseObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string DbName
{
get { return _dbName; }
set {
_dbName = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("dbName");
}
}
private _dbName;
}
的XAML:
<Grid>
<ListBox ItemsSource="{Binding DatabaseObjects}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<MyUserControl Title="{Binding DbName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
謝謝你 - 的ObservableCollection和DataTemplate的是我錯過了 – jav1981 2012-04-09 22:21:20
@ jav1981如果您收到了滿意的回答,請接受拼圖的碎片它。 – 2012-04-13 22:23:49