在一个 WPF 网格单元中有多个控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10131944/
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
Having multiple controls in one WPF grid cell
提问by Shawn
New to WPF and learing through tutorials online and have a couple of questions:
刚接触 WPF 并通过在线教程学习,有几个问题:
(1) I am trying to have multiple buttons of different widths appear side by side in one WPF grid cell. However, they seem to always stack up on each other. What am I missing out on?
(1) 我试图在一个 WPF 网格单元格中并排显示多个不同宽度的按钮。然而,它们似乎总是相互叠加。我错过了什么?
(2) Is it possible to control the absolute starting left position for each button within the grid cell?
(2) 是否可以控制网格单元格内每个按钮的绝对起始左侧位置?
Thanks.
谢谢。
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<ScrollViewer>
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShowGridLines ="True" >
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="60*" />
</Grid.ColumnDefinitions>
<Button Content="Button No. 1" Grid.Row="0" Grid.Column="0" />
<GridSplitter HorizontalAlignment="Center" Width="6"
Grid.Row="0" Grid.Column="1" Grid.RowSpan="3" />
<Button Content="Button No. 4-2" Grid.Row="0" Grid.Column="2" Width="100" />
<Button Content="Button No. 4-1" Grid.Row="0" Grid.Column="2" Width="50" />
</Grid>
</ScrollViewer>
</Page>
Based on the answer from Salvador, this works:
根据萨尔瓦多的回答,这是有效的:
<Button VerticalAlignment="Top" HorizontalAlignment="Left" Content="Button No. 1" Grid.Row="0" Grid.Column="0" Height="100"/>
<GridSplitter HorizontalAlignment="Center" Width="6"
Grid.Row="0" Grid.Column="1" Grid.RowSpan="3" />
<Button VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0" Content="Button No. 4-2" Grid.Row="0" Grid.Column="2" Height="100" Width="100" />
<Button VerticalAlignment="Top" HorizontalAlignment="Left" Margin="400,0,0,0" Content="Button No. 4-1" Grid.Row="0" Grid.Column="2" Height="150" Width="50" />
Thanks!
谢谢!
采纳答案by Salvador Sarpi
You are not setting the VerticalAlignment
or the HorizontalAlignment
property, so by default they are centered.
您没有设置VerticalAlignment
或HorizontalAlignment
属性,因此默认情况下它们居中。
You need to set those properties and use them in combination with the Margin
property of wpf elements.
您需要设置这些属性并将它们与Margin
wpf 元素的属性结合使用。
Take a look at this Introduction to WPF Layout
看看这个WPF 布局简介
回答by Dan Rigby
You may want to use one of the Container Controls(like a Canvas, DockPanel, StackPanel) in the grid position (Row # + Column #) and then place your controls into that.
您可能希望在网格位置(行 # + 列 #)中使用容器控件之一(如Canvas、DockPanel、StackPanel),然后将控件放入其中。
This makes it much easier to layout more than one control in a single grid position.
这使得在单个网格位置布局多个控件变得更加容易。