如何在 wpf 中将滚动查看器大小设置为父级的网格大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15174508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:49:06  来源:igfitidea点击:

How to set the scrollviewer size to the parent's grid size in wpf

c#wpfxaml

提问by SagarDabas

I have a popup control in a grid and I want it to resize to the parent grid size. As you can see the parent grid area is in Green color and the popup is in LightCyan color. The area in the LightCyan should cover the whole green area. And I think I have to set the Scrollviewer width and height to the parent's width and height but that didn't work. Please reply. Thanks

我在网格中有一个弹出控件,我希望它调整到父网格的大小。如您所见,父网格区域为绿色,弹出窗口为浅青色。LightCyan 中的区域应覆盖整个绿色区域。而且我想我必须将 Scrollviewer 的宽度和高度设置为父级的宽度和高度,但这不起作用。请回复。谢谢

I am adding different stuff in the LinePopGrid using code.

我正在使用代码在 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

附图

回答by Rohit Vats

You need to bind PopUp widthand heightto Grid's ActualWidthand ActualHeightrespectively -

您需要绑定弹出widthheight网格的ActualWidthActualHeight分别为-

<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>