如何在WPF中将ScrollViewer的大小设置为父级Grid的大小

3
我在网格中添加了一个弹出控件,希望它能随着父网格的大小进行调整。您可以看到,父网格区域是绿色的,而弹出窗口是淡青色的。淡青色的区域应该覆盖整个绿色区域。我认为我必须将Scrollviewer的宽度和高度设置为父宽度和高度,但这并没有起作用。请回复。谢谢。
我正在使用代码向LinePopGrid中添加不同的内容。
<Grid Background="Green">
<Popup x:Name="LinePopUp" IsOpen="True" Placement="Relative" >
        <ScrollViewer  CanContentScroll="True" VerticalScrollBarVisibility="Auto">
            <Grid Background="LightCyan" x:Name="LinePopUpGrid" />
        </ScrollViewer>
    </Popup>
</Grid>

Attached Picture

1个回答

7
您需要将 PopUp 的宽度和高度绑定到 Grid 的 ActualWidth 和 ActualHeight 上 -
<Grid Background="Green">
        <Popup x:Name="LinePopUp" IsOpen="True" Placement="Relative"
               Width="{Binding ActualWidth,RelativeSource={RelativeSource 
                               Mode=FindAncestor, AncestorType=Grid}}"                   
               Height="{Binding ActualHeight,RelativeSource={RelativeSource
                                Mode=FindAncestor, AncestorType=Grid}}">
            <ScrollViewer CanContentScroll="True"
                          VerticalScrollBarVisibility="Auto">
                <Grid Background="LightCyan" x:Name="LinePopUpGrid" />
            </ScrollViewer>
        </Popup>
 </Grid>

非常感谢,它奏效了。我以类似的方式尝试过,但我使用的是Width和Height而不是ActualHeight和ActualWidth,学到了新东西。谢谢。 - SagarDabas
很高兴能够帮助。要了解“Width”和“ActualWidth”的区别,请参考此链接 - https://dev59.com/iXRB5IYBdhLWcg3weXSX - Rohit Vats
这样做行不通。ActualWidth和ActualHeight不是可通知属性,因此当它们改变时,绑定不会更新。请参见https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.frameworkelement.actualwidth中的说明。 - sjb-sjb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接