2012-04-12 49 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)(行號+列#),然後將你的控件成。

這使得在單個網格位置佈置多個控件變得更容易。

相關問題