【问题标题】:How much space Binding element going to take?绑定元素要占用多少空间?
【发布时间】:2014-08-04 06:21:13
【问题描述】:

以下代码来自我的 studentDeutils.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会占用多少空间?

【问题讨论】:

    标签: xaml windows-phone-8


    【解决方案1】:

    不要考虑高度,让 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>
    

    【讨论】:

    • 这对我不起作用。我将上面的列表框添加到 并将文本块添加到 。但不起作用...
    • 嗨@Johannes Wanzek ...如果我使用 Grid.Row="0" 和 Grid.Row="1" 页面将分开一半。我想根据该列表框大小显示该文本框。
    • 抱歉忘记添加网格关闭标签。第 0 行占用尽可能多的可用空间,网格 1 占用尽可能多的空间。再试一次。它应该工作。
    猜你喜欢
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 2018-07-22
    • 1970-01-01
    相关资源
    最近更新 更多