WPF使用MVVM模式创建动态行

WPF使用MVVM模式创建动态行,wpf,mvvm,datatemplate,Wpf,Mvvm,Datatemplate,我想用动态行加载我的WPF UserControl。我的设想如下 1. When the UserControl gets loaded, I will populate my List<string> object with some values which I will get from a database. 2. I need to create equal number of rows in my UserControl which matches the number

我想用动态行加载我的WPF UserControl。我的设想如下

1. When the UserControl gets loaded, I will populate my List<string> object with some values which I will get from a database.
2. I need to create equal number of rows in my UserControl which matches the number of items in the List<string> object. The display will look something like below
1。加载UserControl后,我将使用从数据库中获取的一些值填充列表对象。
2.我需要在UserControl中创建与列表对象中的项数相同的行数。显示屏将如下所示
列1是标签控件,列2是文本块控件

   Label 1: Item 1 (This is the value from the List<string> object)
   Label 2: Item 2
   Label 3: Item 3

I know how to create rows dynamically but my problem is how do I do this when I'm using the MVVM pattern. 
标签1:项目1(这是列表对象中的值) 标签2:项目2 标签3:项目3 我知道如何动态创建行,但我的问题是在使用MVVM模式时如何做到这一点。 注意:我使用的是来自CodePlex的MVVM工具包

谢谢,
Jithu

将您拥有的MVVM对象设置为UserControl的dataContext,我希望该对象中有一个Collection属性。然后创建一个ItemsControl,如下所示 从您的描述中不清楚标签和项目真正来自ViewModel的位置。下面的代码将动态创建与Collection.Count一样多的行

  <ItemsControl ItemsSource="{Binding YourStringCollection}"  HorizontalAlignment="Left" >
       <ItemsControl.ItemsPanel>   
             <ItemsPanelTemplate>
                     <StackPanel Orientation="Vertical"/>              
             </ItemsPanelTemplate>                               
       </ItemsControl.ItemsPanel>
       <ItemsControl.ItemsTemplate>   
             <DataTemplate>
                     <TextBlock Text="{Binding}">              
             </DataTemplate >                               
       </ItemsControl. ItemsTemplate >
  </ItemsControl>