我是Windows Phone 7的新手。我想生成一个从其他页面注册的学生列表。我想将学生列表显示为网格记录
我的代码如下:
xaml代码:
<Grid VerticalAlignment="Top" HorizontalAlignment="Left" ShowGridLines="True" Width="400" Height="200" Name="GridStud">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock FontSize="20" FontWeight="Bold" Grid.ColumnSpan="3" Grid.Row="0">Registered Student List</TextBlock>
<TextBlock FontSize="12" FontWeight="Bold" Grid.Row="1" Grid.Column="0">Id</TextBlock>
<TextBlock FontSize="12" FontWeight="Bold" Grid.Row="1" Grid.Column="1">Full Name</TextBlock>
<TextBlock FontSize="12" FontWeight="Bold" Grid.Row="1" Grid.Column="2">Contact</TextBlock>
<TextBlock FontSize="12" FontWeight="Bold" Grid.Row="1" Grid.Column="3">Email</TextBlock>
<TextBlock FontSize="12" FontWeight="Bold" Grid.Row="1" Grid.Column="4">Stream</TextBlock>
<TextBlock FontSize="12" FontWeight="Bold" Grid.Row="1" Grid.Column="5">Delete</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0" Name="txtId" />
<TextBlock Grid.Row="2" Grid.Column="1" Name="txtName" />
<TextBlock Grid.Row="2" Grid.Column="2" Name="txtContact" />
<TextBlock Grid.Row="2" Grid.Column="3" Name="txtEmail" />
<TextBlock Grid.Row="2" Grid.Column="4" Name="txtStream" />
<Button FontSize="12" FontWeight="Bold" Grid.Row="2" Grid.Column="5" Name="btnDelete">Delete</Button>
<TextBlock FontSize="16" FontWeight="Bold" Grid.ColumnSpan="3" Grid.Row="4">Total Students: 3</TextBlock>
</Grid>
CS代码:
private void PivotItem_Loaded(object sender, RoutedEventArgs e)
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
StreamReader sr = null;
try
{
sr = new StreamReader(new IsolatedStorageFileStream("Data\\inquiry.json", FileMode.Open, isf));
while (!sr.EndOfStream)
{
var stud = JsonConvert.DeserializeObject<Student>(sr.ReadLine());
txtId.Text += "\t" + stud.Id;
txtName.Text += "\t" + stud.Name;
txtContact.Text += "\t" + stud.Contact;
txtEmail.Text += "\t" + stud.Email;
txtStream.Text += "\t" + stud.Stream;
}
sr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
显示所有已注册的记录,但是在一行中。我想在不同的行中显示这些记录,并生成运行时网格行。
答案 0 :(得分:0)
public ObservableCollection<StudentListModel> StudentListItems { get; private set; }
初始化ObservableCollection。并且您必须在StudentListModel类中指定字段。喜欢关注
private long? _id;
public long? Id
{
get
{
return _id;
}
set
{
if (value != _id)
{
_id = value;
NotifyPropertyChanged("Id");
}
}
}
在PhoneApplicationPage的构造函数中,您必须创建如下所示的StudentListItem
public StudentList()
{
this.StudentListItems = new ObservableCollection<StudentListModel>();
}
之后您必须将值分配给集合,如下所示。在这里,我从数据库中检索值。
foreach (var dri in getStudentList)
{
this.StudentListItems.Add(new StudentListModel()
{
Id= dri.Id,
Name= dri.Name,
Contact= dri.Contact,
Email= dri.Email,
});
}
你必须将它设置为Listbox。
viewStudentList.ItemsSource = StudentListItems;
您的观点应该如下。它给出了输出列表。
<ListBox ItemsSource="{Binding}" Name="viewStudentList" Grid.ColumnSpan="3" Margin="7,38,-7,-108" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="40">
<TextBlock Text="{Binding ID}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" Margin="0,0,290,-22" TextAlignment="Left" Foreground="{StaticResource PhoneForegroundBrush}"/>
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" Foreground="{StaticResource PhoneForegroundBrush}" Style="{StaticResource PhoneTextSubtleStyle}" Margin="60,0,138,-22" TextAlignment="Center"/>
<TextBlock Text="{Binding Contact}" TextWrapping="Wrap" Foreground="{StaticResource PhoneForegroundBrush}" Style="{StaticResource PhoneTextSubtleStyle}" Margin="150,0,12,-22" TextAlignment="Center"/>
<TextBlock Text="{Binding Email}" TextWrapping="Wrap" Foreground="{StaticResource PhoneForegroundBrush}" Style="{StaticResource PhoneTextSubtleStyle}" Margin="355,0,12,-22" TextAlignment="Right"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>