2016-07-27 172 views
2

這裏是父的Xaml:爲什麼UserControl沒有填充VerticalContentAlignment'Stretch'?

<Grid VerticalAlignment="Stretch"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="20" /> 
     <RowDefinition Height="*" /><!-- Should stretch vertically --> 
    </Grid.RowDefinitions> 
    <DockPanel Grid.Row="0"> 
     <!-- Menu definition removed for brevity --> 
    </DockPanel> 
    <DockPanel Grid.Row="1" 
       VerticalAlignment="Stretch" 
       LastChildFill="True"> 
     <ItemsControl DockPanel.Dock="Top" 
         ItemsSource="{Binding Designer}" 
         HorizontalAlignment="Stretch" 
         HorizontalContentAlignment="Stretch" 
         VerticalAlignment="Stretch" 
         VerticalContentAlignment="Stretch" 
         Background="YellowGreen"/> 
    </DockPanel> 
</Grid> 

ItemsSource結合是一個ObservableCollection。當視圖添加到集合時,它會在主外殼(視圖)中更新。這裏是UserControl被添加:

<UserControl x:Class="Prototype.StateMachineDesignerApp.Views.ProjectDesigner" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:Prototype.StateMachineDesignerApp.Views" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="500" 
      HorizontalAlignment="Stretch" 
      HorizontalContentAlignment="Stretch" 
      VerticalAlignment="Stretch" 
      VerticalContentAlignment="Stretch" 
      Margin="0"> 
    <Grid VerticalAlignment="Stretch"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="20" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
     <Border Grid.Row="0" 
       BorderBrush="DimGray" 
       BorderThickness="1" 
       Background="LightSlateGray" 
       Margin="1" /> 
     <Border Grid.Row="1" 
       Margin="1"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="1*" /> 
        <ColumnDefinition Width="3" /> 
        <ColumnDefinition Width="5*" /> 
       </Grid.ColumnDefinitions> 
       <Border Grid.Column="0" 
         BorderBrush="DarkGoldenrod" 
         BorderThickness="1" /> 
       <GridSplitter Grid.Column="1" 
           HorizontalAlignment="Stretch" 
           VerticalAlignment="Stretch" /> 
       <Border Grid.Column="2" 
         BorderBrush="DarkGoldenrod" 
         BorderThickness="1" /> 
      </Grid> 
     </Border> 
    </Grid> 
</UserControl> 

UserControl未完全填滿整個垂直空間: user control image

最外GridRow[1]被垂直拉伸如由ItemsControl.Background證明填充所述區域。

以供參考,這是我期待: enter image description here

爲了什麼它的價值,我已經看了許多問題上的SO關於這個確切問題,但沒有解決方案似乎幫助。再次,我看到的例子都沒有使用帶有綁定的ItemsControl

+0

從什麼時候我們沒有允許WPF問題?對於我沒有意識到的SO有過改變嗎? – IAbstract

+0

我沒看到你得到的東西有什麼問題,從我的結局來看沒問題。我確實看到一個Grid.Row在父類上定義,但是......那個父類的任何機會都將該行設置爲Auto height或其他任何東西?我會再看。 –

+0

我並不關心反對票。這是投票結束有關我(可能是由同一個人)。無論如何,我會添加整個父網格定義。 – IAbstract

回答

2

發生這種情況是因爲ItemsControl默認情況下會將我們所有的物品放到垂直對齊的StackPanel中。雖然這很容易更改,因爲ItemsControl允許您更改用於容納所有項目的面板類型。

<ItemsControl DockPanel.Dock="Top" 
        ItemsSource="{Binding Designer}" 
        HorizontalAlignment="Stretch" 
        HorizontalContentAlignment="Stretch" 
        VerticalAlignment="Stretch" 
        VerticalContentAlignment="Stretch" 
        Background="Transparent"> 
    <ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <Grid /> 
    </ItemsPanelTemplate> 
</ItemsControl.ItemsPanel> 
</ItemsControl> 
+0

!!哇! ...這可以使用更好的文檔。固定! – IAbstract