关注我的studentDeatils.xaml页面的代码。我有
<ListBox ItemsSource="{Binding}" Name="StudentDetails">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="40">
<TextBlock Text="{Binding StudentName, StringFormat='t'}" TextWrapping="Wrap" Margin="2,0,12,10" TextAlignment="Center"/>
<TextBlock Text="{Binding StudentRegNo, StringFormat='t'}" TextWrapping="Wrap" Margin="85,0,12,10" TextAlignment="Center"/>
<TextBlock Text="{Binding Address, StringFormat='t'}" TextWrapping="Wrap" Margin="175,0,12,10" TextAlignment="Center"/>
<TextBlock Text="{Binding TWD, StringFormat='t'}" TextWrapping="Wrap" Margin="365,0,12,10" TextAlignment="Center"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock x:Name="totalstudents" FontSize="24" TextAlignment="Center" FontWeight="Bold" Text="" TextWrapping="Wrap" HorizontalAlignment="Left" Width="247" />
我使用来自studentDeatils.xaml.cs的以下代码将数据发送到.xaml页面。这里StudentDetailsViewer类用于显示绑定文件。
foreach (var stu in Studentdetails)
{
this.StudentsheetItems.Add(new StudentDetailsViewer()
{
StudentName = stu.StudentName,
StudentRegNo = stu.StudentRegNo,
Address= stu.Address,
TWD = stu.TWD
});
};
StudentDetails.ItemsSource = StudentsheetItems;
我想在绑定列表后查看TextBlock(x:Name =“totalstudents”)。如何知道Listbox将采用多少空间绑定?
答案 0 :(得分:0)
不要考虑高度,让XAML为你做。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.Rowdefinitions>
<ListBox Grid.Row="0" ItemsSource="{Binding}" Name="StudentDetails">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="40">
<TextBlock Text="{Binding StudentName, StringFormat='t'}" TextWrapping="Wrap" Margin="2,0,12,10" TextAlignment="Center"/>
<TextBlock Text="{Binding StudentRegNo, StringFormat='t'}" TextWrapping="Wrap" Margin="85,0,12,10" TextAlignment="Center"/>
<TextBlock Text="{Binding Address, StringFormat='t'}" TextWrapping="Wrap" Margin="175,0,12,10" TextAlignment="Center"/>
<TextBlock Text="{Binding TWD, StringFormat='t'}" TextWrapping="Wrap" Margin="365,0,12,10" TextAlignment="Center"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock x:Name="totalstudents" Grid.Row="1" FontSize="24" TextAlignment="Center" FontWeight="Bold" Text="" TextWrapping="Wrap" HorizontalAlignment="Left" Width="247" />
</Grid>