XAML 动态设置ScrollViewer高度

r6vfmomb  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(157)

我目前正在开发一个WinUI 3应用程序,我试图在ScrollViewer的上方和下方设置一个StackPanel,它将始终可见(不取决于屏幕的大小或ScrollViewer中的元素):

<Grid Margin="30,30,30,30">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
                
    <StackPanel Orientation="Horizontal" Grid.Row="0">
        <TextBlock Name="projectNumber" Text="Project Number" Width="100" Margin="10,10,10,10" TextAlignment="Center"/>
        <TextBlock Name="taskName" Text="Task" Width="750" Margin="10,10,10,10" TextAlignment="Center"/>
        <TextBlock Name="dueDate" Text="Due Date" Width="100" Margin="10,10,10,10" TextAlignment="Center"/>
        <TextBlock Name="assignedBy"  Text="Assigned By" Width="100" Margin="10,10,10,10" TextAlignment="Center"/>
    </StackPanel>
    <Line Name="dividerLine" Stroke="DimGray" X1="0" Y1="0" X2="1130" Y2="0" Grid.Row="1"/>
    <ScrollViewer Name="scrollViewer" Grid.Row="2">
        <StackPanel>
            <ListView Name="tasksPanel" VerticalAlignment="Top" HorizontalAlignment="Left"/>
            <ListView Name="completeTasksPanel" VerticalAlignment="Top" HorizontalAlignment="Left"/>
        </StackPanel>
    </ScrollViewer>
    <StackPanel Orientation="Horizontal" Margin="0,10,0,10" Grid.Row="3">
        <TextBox/>
        <TextBox/>
        <TextBox/>
    </StackPanel>
</Grid>

字符串
但是,ScrollViewer元素不会根据屏幕设置其高度,相反,它将占用与内部元素相同的空间。我还尝试将Grid.RowDefinitions设置为设置高度,而不是Auto,但没有区别:

<RowDefinition Height="50"/>
<RowDefinition Height="3"/>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>

sdnqo3pr

sdnqo3pr1#

你应该把ScrollViewer中的StackPanel替换为另一个Panel,它不会用无限的垂直空间来度量其子元素,例如Grid

<ScrollViewer Name="scrollViewer" Grid.Row="2">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ListView Name="tasksPanel" VerticalAlignment="Top" HorizontalAlignment="Left"/>
        <ListView Grid.Row="1" Name="completeTasksPanel" VerticalAlignment="Top" HorizontalAlignment="Left"/>
    </Grid>
</ScrollViewer>

字符串
另一个选项是为StackPanel指定固定高度。
ScrollViewersStackPanels不能很好地一起工作,因为后者的(无限测量)特性。

相关问题