【问题标题】:ItemsControl does not stretch verticallyItemsControl 不垂直拉伸
【发布时间】:2015-09-17 12:09:44
【问题描述】:

我有一个 PRISM v5 应用程序,给我带来了一些麻烦。

我的UserControlGrid 中有三个ItemsControls

<Grid VerticalAlignment="Stretch">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="1*" MinWidth="50"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" MinHeight="50"/>
        <RowDefinition MinHeight="50" />
    </Grid.RowDefinitions>

    <!--Header region-->
    <ItemsControl
        Grid.Row="0"
        Grid.Column="0"
        Grid.ColumnSpan="2"
        regions:RegionManager.RegionName="{x:Static infrastructure:RegionNames.HeaderRegion}"/>

    <!--Main region-->
    <ItemsControl
        Grid.Row="1"
        Grid.Column="0"
        regions:RegionManager.RegionName="{x:Static infrastructure:RegionNames.MainRegion}">
    </ItemsControl>

    <!--Menu region-->
    <ItemsControl
        Background="DarkGoldenrod"
        Grid.Row="1"
        Grid.Column="1"
        regions:RegionManager.RegionName="{x:Static infrastructure:RegionNames.MenuRegion}">
    </ItemsControl>

当我运行它们并插入未指定高度的视图时,它们不会填满所有可用空间。 但是它们填充了水平空间(宽度),如下所示:

我看到了很多这样的建议:

    <ItemsControl
        Grid.Row="1"
        Grid.Column="0"
        regions:RegionManager.RegionName="{x:Static infrastructure:RegionNames.MainRegion}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid></Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

但它不起作用,因为我收到此错误消息:

System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='SecondView'
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='SecondView'

在进行新搜索之前,我什至已经在 Google 上搜索到了第 4 页 - 我真的非常渴望能够将其垂直延伸的东西。

更新 1:

所以我将ItemsControl 之一更改为ContentControl

<!--Header region-->
<ItemsControl
    Grid.Row="0"
    Grid.Column="0"
    regions:RegionManager.RegionName="{x:Static infrastructure:RegionNames.HeaderRegion}"/>

<!--Main region-->
<ContentControl
    Grid.Row="1"
    Grid.Column="0"
    VerticalAlignment="Stretch"
    VerticalContentAlignment="Stretch"
    regions:RegionManager.RegionName="{x:Static infrastructure:RegionNames.MainRegion}"/>

我将我的SecondView 加载到ContentControl 中,这是一个UserControl,内容如下:

<Grid
    Background="IndianRed"
    VerticalAlignment="Stretch"
    HorizontalAlignment="Stretch"
    MinHeight="200"
    MinWidth="200"/>

我是这样做的:

var obj = _container.Resolve<SecondView>();
var region = _manager.Regions[RegionNames.MainRegion];
region.Add(obj);

现在看起来像这样:

仍然没有拉伸到页面底部。

更新 2: 事实证明ContentControl 是这个问题的答案。 但是你不能有一个未指定的高度。我有以下内容:

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" MinHeight="50"/>
        <RowDefinition />
    </Grid.RowDefinitions>

Height 更改为第二个RowDefinition 会得到想要的结果。但现在我需要知道它必须有多高,因为它不会扩展。

所以我在其他地方寻找解决方案。 有人(在某个地方,我不记得在哪里)说这可能会奏效:

<Grid.Resources>
    <resources:VisibilityToStarHeightConverter x:Key="VisibilityToStarHeightConverter"/>
</Grid.Resources>

<Grid.RowDefinitions>
    <RowDefinition Height="Auto" MinHeight="50" />
    <RowDefinition Height="{Binding Converter={StaticResource VisibilityToStarHeightConverter}, ConverterParameter=5}" />
</Grid.RowDefinitions>

将它绑定到这样的类:

public class VisibilityToStarHeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((Visibility)value == Visibility.Collapsed)
        {
            return new GridLength(0, GridUnitType.Star);
        }
        else
        {
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            return new GridLength(double.Parse(parameter.ToString(), culture), GridUnitType.Star);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

但是如果没有在(Visivility)value 上获得InvalidCastException,我就无法加载应用程序

有人知道如何动态查看窗户的高度吗?

【问题讨论】:

标签: wpf prism itemscontrol stretch


【解决方案1】:

ItemsControl 被垂直拉伸,但您的视图被添加为 ItemsControl 中的项目。

请在您的地区使用 ContentControl

【讨论】:

  • 修复起来很麻烦(不止这个),但我会试一试并报告。
  • 它仍然不能与 ContentControl 一起使用。你有更多建议吗?
  • 让我收回。发现我的身高设置错误。
  • 让我也纠正一下 - 如果我指定一个高度,它会起作用(但我不想要那个)。它仍然不起作用。您还有其他建议吗?
【解决方案2】:

尝试将您的网格行定义为拉伸,如下所示:

<Grid.RowDefinitions>
    <RowDefinition Height="Auto" MinHeight="50"/>
    <RowDefinition Height="*" MinHeight="50" />
</Grid.RowDefinitions>

【讨论】:

  • 与更新 1 中的结果相同。尝试查看我的更新 2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
  • 2018-02-25
  • 2010-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多