wpf 拉伸项目以填充画布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13621041/
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
Stretch items to fill canvas
提问by JosephGarrone
I have a Dockpanel with items inside a Canvas. The Dockpanel and any other items (Grid etc) that I place inside the Canvas, only take up their minimum required space. How do I stretch these items to fill the entire Canvas?
我有一个 Dockpanel,里面有一个 Canvas 里面的项目。我放置在画布内的 Dockpanel 和任何其他项目(网格等)只占用它们所需的最小空间。如何拉伸这些项目以填充整个 Canvas?
<Canvas x:Name="InfoCanvas" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="72,53,0,0">
<DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0,0,0,0" x:Name="ReferenceInfo" Canvas.Left="0" Canvas.Top="0">
<TextBox x:Name="ReferenceAuthor" GotFocus="FieldEnter" LostFocus="FieldLeave" FontSize="16" FontFamily="Segoe UI Light" Text="Author" Foreground="Gray" Background="Transparent" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Stretch" BorderThickness="0" Margin="0,2,0,2"/>
<TextBox x:Name="ReferenceTitle" GotFocus="FieldEnter" LostFocus="FieldLeave" FontSize="16" FontFamily="Segoe UI Light" Text="Title" Foreground="Gray" Background="Transparent" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Stretch" BorderThickness="0" Margin="0,2,0,2"/>
<TextBox x:Name="ReferenceDate" GotFocus="FieldEnter" LostFocus="FieldLeave" FontSize="16" FontFamily="Segoe UI Light" Text="Date" Foreground="Gray" Background="Transparent" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Stretch" BorderThickness="0" Margin="0,2,0,2"/>
</DockPanel>
</Canvas>
Thank you!
谢谢!
回答by Peter Hansen
The Canvaspanel doesn't really support that.
该Canvas小组并不真正支持这一点。
It's very basic - it just allows you to position children absolutely by using Top, Bottom, Left and Right, and it always gives them just the space they need.
这是非常基本的 - 它只允许您通过使用顶部、底部、左侧和右侧来绝对定位孩子,并且它始终为他们提供他们需要的空间。
So usually you would use a Gridwith just 1 column and 1 row instead.
所以通常你会使用Grid只有 1 列和 1 行的 a 来代替。
You can however bind the width and height of the DockPanelto the width and height of the Canvas. That way the DockPanelwill always fill the Canvas.
但是,您可以将 的宽度和高度绑定DockPanel到 的宽度和高度Canvas。这样DockPanel将始终填充Canvas.
<DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
Margin="0,0,0,0" x:Name="ReferenceInfo" Canvas.Left="0" Canvas.Top="0"
Width="{Binding ActualWidth, ElementName=InfoCanvas}"
Height="{Binding ActualHeight, ElementName=InfoCanvas}">
回答by Johan Larsson
What you can do is:
你可以做的是:
<Grid>
<Canvas x:Name="InfoCanvas">
<!--Elements with canvas layout here-->
</Canvas>
<DockPanel x:Name="ReferenceInfo">
<!--Elements with dockpanel layout here-->
</DockPanel>
</Grid>
By wrapping both panels in a grid like this you can place elements that you cant to position relative to left, top etc in the canvas. Both the canvas and the dockpanel will fill available space. Note that the elements in the dockpanel will be rendered above the elements in the canvas when the dockpanel is defined after in xaml.
通过将两个面板包裹在这样的网格中,您可以将无法相对于左侧、顶部等定位的元素放置在画布中。画布和停靠面板都将填充可用空间。请注意,当在 xaml 之后定义停靠面板时,停靠面板中的元素将呈现在画布中的元素上方。
I'm assuming the code you posted is pseudo code, if not you should just remove the canvas.
我假设您发布的代码是伪代码,否则您应该删除画布。

