如何将datatemplate绑定到observablecollection以便它们都显示出来?

时间:2014-11-02 22:19:53

标签: c# wpf xaml binding datatemplate

如何绑定数据窗口以为observablecollection中的每个项目重现自身,以便它们都显示出来?或者我必须在运行时将它们作为单独的控件添加吗?

所以我有一个datatemplate:

<UserControl.Resources>
    <DataTemplate x:Key="QueueItem">
        <StackPanel Orientation="Vertical">
            <Label Content="{Binding caseNumber}"></Label>
            <!--TODO: put my other controls for this template here...-->
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

我在xaml中实现它:

<ContentControl
               Content="{Binding TestItems}" 
               ContentTemplate="{StaticResource QueueItem}">
</ContentControl>

在我的viewmodel中有一个我做的某个类的TestItems实例,其中一个成员是caseNumber,所以上面的代码对我来说很好......但是现在我希望有一个可观察的集合相同的类,是否有可能/是否有xaml语法绑定到集合实例以重现上面的每个索引?

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用ItemsControl并使用ItemsTemplate属性作为模板:

<ContentControl>
     <ItemsControl
           ItemsSource="{Binding YourTestItemList}" 
           ItemTemplate="{StaticResource QueueItem}"/>
</ContentControl>

答案 1 :(得分:1)

使用ItemsControl代替ContentControl

Click here for sample code