2016-07-27 177 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