WPF StackPanel 自动调整大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12254670/
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
WPF StackPanel autosize
提问by user3357963
I have a Canvasnamed mainCanvas and I'm programatically adding a ScrollViewerand a StackPanelto it in order of
我有一个Canvas名为 mainCanvas 并且我正在以编程方式将 aScrollViewer和 a添加StackPanel到它的顺序中
...mainCanvas
...主画布
......scrollView
......滚动视图
.........pnl
.......pnl
............ (more stacked controls)
…………(更多堆叠控件)
I'm attempting to get my StackPanel to auto size to the mainCanvas size and allow scrolling when it's too large. Code so far is below
我试图让我的 StackPanel 自动调整到 mainCanvas 的大小,并在它太大时允许滚动。到目前为止的代码如下
mainCanvas.Children.Clear();
// Create the container
ScrollViewer scrollView = new ScrollViewer();
scrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
scrollView.CanContentScroll = true;
scrollView.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
scrollView.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
StackPanel pnl = new StackPanel();
//pnl.Height = 500; //Works and allows scrolling but doesn't resize
pnl.Height = Double.NaN; //(Double.NaN is Auto) Doesn't Work - StackPanel overflows parent window
pnl.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
pnl.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
scrollView.Content = pnl;
// Add the ScrollView and StackPanel to Parent Window
mainCanvas.Children.Add(scrollView);
Unfortunately, the StackPanel doesn't fit to the parent and doesn't autosize.
不幸的是,StackPanel 不适合父级并且不会自动调整大小。
mainCanvasalready exists in XAML with settings:
mainCanvas已存在于 XAML 中并带有设置:
Width= "Auto"
宽度=“自动”
Height= "Auto"
高度=“自动”
HorizontalAlignment = "Stretch"
水平对齐 =“拉伸”
VerticalAlignment = "Stretch"
VerticalAlignment = "拉伸"
I can get things semi-working by using pnl.Height = 500;which shows me the scrollbar does work if the Stackpanel height is restricted. But this is just manually fitting the height to the full screen size and so doesn't autosize when resizing the app.
pnl.Height = 500;如果 Stackpanel 高度受到限制,我可以通过使用它来显示滚动条确实可以工作,从而使事情处于半工作状态。但这只是手动将高度调整到全屏大小,因此在调整应用程序大小时不会自动调整大小。
I hoped setting pnl.Height = Double.NaN;to auto and V/H adjustment to Stretch would work but the StackPanel still overlaps all controls to it's maximum size.
我希望将pnl.Height = Double.NaN;自动和 V/H 调整设置为 Stretch 会起作用,但 StackPanel 仍然将所有控件重叠到它的最大尺寸。
Can anyone point me in the right direction to get my StackPanel to fit the parent mainCanvas and autosize when I resize either the parent and/or the main app window with scrolling?
任何人都可以指出我正确的方向以使我的 StackPanel 适合父级 mainCanvas 并在我通过滚动调整父级和/或主应用程序窗口的大小时自动调整大小?
Thank you
谢谢
回答by a little sheep
I believe Canvas is for absolute positioning only. Using a Grid as your panel instead would probably give you the desired result.
我相信 Canvas 仅用于绝对定位。使用 Grid 作为您的面板可能会给您带来所需的结果。
回答by McGarnagle
The StackPanelwon't stretch to fill its container, as you noticed. But you can bind its MinWidthand MinHeightproperties to its container's width/height.
该StackPanel不会延伸到填充其容器,因为你注意到。但是您可以将其MinWidth和MinHeight属性绑定到其容器的宽度/高度。
// give the canvas a name, so you can bind to it
mainCanvas.Name = "canvas";
// create the binding for the Canvas's "ActualHeight" property
var binding = new System.Windows.Data.Binding();
binding.ElementName = "canvas";
binding.Path = new PropertyPath("ActualHeight");
// assign the binding to the StackPanel's "MinHeight" dependency property
sp.SetBinding(StackPanel.MinHeightProperty, binding);

