2012-04-12 63 views
4

新的WPF多个控件,并通过辅导性学习的网上,并有几个问题:有一个WPF网格单元

(1)我想有不同宽度的多个按钮在一个WPF并排显示网格单元格。但是,他们似乎总是互相堆叠在一起。我错过了什么?

(2)是否可以控制网格单元格内每个按钮的绝对起始左侧位置?

谢谢。

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

基于来自萨尔瓦多的答案,这个工程:

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

谢谢!

回答

4

您没有设置VerticalAlignmentHorizontalAlignment属性,所以默认情况下它们是居中。

您需要设置这些属性,并将它们与wpf元素的Margin属性结合使用。

看一看这个Introduction to WPF Layout

+0

完美,工作只是我想要的方式: – Shawn 2012-04-12 21:32:58

6

你可能想在网格位置使用的Container Controls一个(如CanvasDockPanelStackPanel)(行号+列#),然后将你的控件成。

这使得在单个网格位置布置多个控件变得更容易。

相关问题