为什么UserControl没有填充VerticalContentAlignment'Stretch'?

时间:2016-07-27 17:59:00

标签: c# wpf xaml user-controls

这是父Xaml:

<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。将视图添加到集合后,它将在主shell(视图)中更新。以下是添加的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

最外层Row[1]

Grid垂直伸展,ItemsControl.Background填充该区域就是明证。

供参考,这是我所期待的: enter image description here

为了它的价值,我已经就这个确切的问题看了很多关于SO的问题,但没有一个解决方案似乎有所帮助。然后,我看到的所有例子都没有使用带有绑定的ItemsControl

1 个答案:

答案 0 :(得分: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>