【发布时间】:2015-09-17 12:09:44
【问题描述】:
我有一个 PRISM v5 应用程序,给我带来了一些麻烦。
我的UserControl 的Grid 中有三个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,我就无法加载应用程序
有人知道如何动态查看窗户的高度吗?
【问题讨论】:
-
视图的内容是什么?有些控件根本不会拉伸。最好的例子是 Stackpanel。在垂直方向上,它不会超出其子项所需的最小垂直空间。
-
我使用背景颜色设置为绿色的
Grid,使用红色@Gusdor 的Grid
标签: wpf prism itemscontrol stretch