在ScrollViewer中显示用户控件

时间:2012-08-08 21:23:17

标签: c# user-controls microsoft-metro scrollviewer

我有一个滚动查看器,我想显示所有用户控件。我有两种类型的用户控件,即教育和工作。它们只是文本框和按钮。我动态创建各自的数字,并将它们添加到usercontrol类型的列表中。但是我无法显示所有用户控件!

Scroll Viewer xaml

<ScrollViewer x:Name="myScrollViewer" HorizontalAlignment="Left" Height="518" Margin="128,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="1123" FontSize="22"/>

背后的代码

List<UserControl> info = new List<UserControl>();
EducationControl educationControl = new EducationControl(school._school, school._degree, school._fieldOfStudy, school._educationStartDate, school._educationEndDate);
info.Add(educationControl);
WorkControl workControl = new WorkControl(work._employer, work._jobTitle, work._jobStartDate, work._jobEndDate);
info.Add(workControl);
myScrollViewer.Content = info;

当我将scrollviewer的内容设置为info时,它只显示如下字符串: System.Collections.Generic.list'1 [Windows.UI.Xaml.Controls.UserControl]

然而,当我这样做时,我可以让个人用户控件正确显示:

myScrollViewer.Content = info.ElementAt(0);

如何在滚动查看器中显示所有用户控件?

1 个答案:

答案 0 :(得分:1)

ScrollViewer的内容必须是列表样式的控件,例如ListBox。 ScrollViewer本身不知道如何呈现List<UserControl>,因此只打印类型名称。

您可能希望items成为ItemsSource或类似控件的ListBox