2014-10-19 63 views
0

在用戶控件,這我嵌入我的主視圖, 我已經定義了以下佈局:WPF:矩形網格忽略保證金

<Grid x:Name="RootGrid" Margin="0,10,0,0" HorizontalAlignment="Stretch"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="30*" /> 
      <ColumnDefinition Width="20*" /> 
      <ColumnDefinition Width="20*" /> 
      <ColumnDefinition Width="30*" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 

     <Rectangle Margin="5,0,5,0" 
        Grid.RowSpan="5" 
        Grid.ColumnSpan="4"    
        Panel.ZIndex="-1" 
        Stroke="Blue" 
        Fill ="Black" 
        StrokeThickness="2" 
        /> 

在Visual Studio中的預覽,它看起來像預期的那樣: - 這是保證金5(用於正確的調整)被考慮在內。

不幸的是,在運行期間它是另一回事。我可以根據需要設置正確的調整(邊距),矩形的右邊框缺失。

有人可以告訴我,我在這裏做錯了什麼?我不想爲絕對寬度的矩形(這是工作)。

更新:

根據艾爾諾的建議,我用了一個邊界(這確實要簡單得多):

<Border HorizontalAlignment="Stretch" Margin="10,0,10,0" Style="{StaticResource StatusPanelBorder}"> 
     <Grid x:Name="RootGrid" HorizontalAlignment="Stretch"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="30*" /> 
       <ColumnDefinition Width="20*" /> 
       <ColumnDefinition Width="20*" /> 
       <ColumnDefinition Width="30*" /> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto" /> 
       <RowDefinition Height="Auto" /> 
       <RowDefinition Height="Auto" /> 
       <RowDefinition Height="Auto" /> 
       <RowDefinition Height="Auto" /> 
      </Grid.RowDefinitions> 

但問題仍然是相同的。 我在主視圖中,其具有以下佈局嵌入這樣的觀點:

<Grid Width="1600" Height="Auto" HorizontalAlignment="Stretch" Background="{StaticResource NoiseBackground}"> 
     <Grid.ColumnDefinitions> 

      <ColumnDefinition Width="30*" /> 
      <ColumnDefinition Width="90*" /> 
      <ColumnDefinition Width="40*" /> 

     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="600" /> 
      <RowDefinition Height="100" /> 
     </Grid.RowDefinitions> 

的子視圖被嵌入到網格的最後一列。 如果我不使用'拉伸'對齊它的作品,但我想'拉伸'的UI元素。

第二次更新: 問題在於shell視圖的寬度較小。問題解決了!

回答

2

添加邊框到網格的最好辦法是採取邊框和巢格在它的內部:

<Border> 
    <Grid> 
     ... 
    </Grid> 
</Border> 

這樣的網格和邊界將調整你可能想要的方式,你可以控制邊距,並且不必將文本塊的邊距與矩形的邊框保持同步。

編輯 看着你加入到我猜你的窗口的寬度設置爲1600。如果是的話,電網的寬度也設置1600不適合,因爲寬度問題的XAML窗口包括左邊界和右邊界。因此,將網格寬度設置爲1600會將其切斷。

我的建議:不要使用硬編碼大小,對列和行使用星形大小,並使用窗口最大化;網格會自動拉伸它們的內容。

+0

Thx爲邊界提示。但問題仍然存在:(...查看我的更新描述 – Marco 2014-10-19 15:51:08

+0

這讓我很尷尬,但問題只是在我的外殼窗口中,寬度小於1600 ...有時你看不到樹木。 ...反正thx爲你的時間! – Marco 2014-10-20 16:56:36

+0

@ Hawk66 - 沒問題,只記得:不要使用硬代碼值。 – 2014-10-20 17:17:40