基本上,您需要一个枚举控件,它对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