【问题标题】:Why UserControl isn't filling VerticalContentAlignment 'Stretch'?为什么 UserControl 不填充 VerticalContentAlignment 'Stretch'?
【发布时间】:2016-07-27 17:59:00
【问题描述】:

这是父 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。当视图添加到集合中时,它会在主外壳(视图)中更新。这是添加的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 没有填满整个垂直空间:

最外面的GridRow[1] 正在垂直伸展,ItemsControl.Background 填充该区域就证明了这一点。

作为参考,这是我所期望的:

对于它的价值,我已经查看了关于这个确切问题的许多关于 SO 的问题,但似乎没有一个解决方案有帮助。再说一次,我看到的例子都没有使用 ItemsControl 绑定。

【问题讨论】:

  • 我们什么时候不允许 WPF 提问了?是否有一些我不知道的更改?
  • 我没有发现你得到的东西有什么问题,从我的角度来说没问题。我确实看到在父级上定义了一个 Grid.Row ......该行的父级是否有可能将该行设置为自动高度或任何东西?我会再往上看。
  • 我不关心否决票。这是关闭与我有关的投票(可能是同一个人)。无论如何,我将添加整个父网格定义。
  • @ChrisW.:更新了最外层的网格定义。

标签: c# wpf xaml user-controls


【解决方案1】:

发生这种情况是因为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>

【讨论】:

  • !!哇!! ...这可以使用更好的文档。固定!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多