我正在尝试创建一个相当复杂的窗口,其中有多个部分显示了一些数据列表。现在,根据来源,每个部分可以在列表中包含一个或多个项目。如果可能,最好每个列表缩小到数据量,但如果空间用完,则在每个ListView中显示滚动条。
我认为网格应该包含ListView。我缺少什么想法?
下面是一个非常简单的例子,也没有这样做,我不知道为什么。
<Window x:Class="Test.ListTestWindow1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="ListTestWindow1" Height="400" Width="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center">
<Button Content="Some Stuff"/>
<TextBlock Text="More Stuff" />
<Button Content="Place holder"/>
</StackPanel>
</Grid>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="List Header Group 1" />
<ListView Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn Width="120" Header="Date" />
<GridViewColumn Width="120" Header="Day Of Week" DisplayMemberBinding="{Binding DayOfWeek}" />
<GridViewColumn Width="120" Header="Year" DisplayMemberBinding="{Binding Year}" />
</GridView>
</ListView.View>
<sys:DateTime>1/2/3</sys:DateTime>
<sys:DateTime>4/5/6</sys:DateTime>
<sys:DateTime>7/8/9</sys:DateTime>
<sys:DateTime>10/11/12</sys:DateTime>
</ListView>
</Grid>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="List Header Group 2" />
<ListView Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn Width="120" Header="Date" />
<GridViewColumn Width="120" Header="Day Of Week" DisplayMemberBinding="{Binding DayOfWeek}" />
<GridViewColumn Width="120" Header="Year" DisplayMemberBinding="{Binding Year}" />
</GridView>
</ListView.View>
<sys:DateTime>1/2/3</sys:DateTime>
<sys:DateTime>4/5/6</sys:DateTime>
<sys:DateTime>7/8/9</sys:DateTime>
<sys:DateTime>10/11/12</sys:DateTime>
</ListView>
</Grid>
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="List Header Group 3" />
<ListView Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn Width="120" Header="Date" />
<GridViewColumn Width="120" Header="Day Of Week" DisplayMemberBinding="{Binding DayOfWeek}" />
<GridViewColumn Width="120" Header="Year" DisplayMemberBinding="{Binding Year}" />
</GridView>
</ListView.View>
<sys:DateTime>1/2/3</sys:DateTime>
<sys:DateTime>4/5/6</sys:DateTime>
<sys:DateTime>7/8/9</sys:DateTime>
<sys:DateTime>10/11/12</sys:DateTime>
</ListView>
</Grid>
</Grid>
</Grid>
</Window>
答案 0 :(得分:1)
如果将RowDefinition的Height
设置为Auto
,那么它将扩展到必要的范围以容纳行内的任何内容。因此,您需要在MaxHeight
或其内容(内部网格)上设置RowDefinition
以限制它可以扩展的数量。
如果您事先知道,可以将MaxHeight设置为静态金额,但更有可能您希望将其设置为其容器的百分比(例如Window
)。为此,您可以绑定到容器的ActualHeight
属性。例如:
<Window x:Name="window" xmlns:clr="clr-namespace:System;assembly=mscorlib">
<Grid Name="grid">
<Grid.Resources>
<local:HeightConverter x:Key="HeightConverter" />
<clr:Int32 x:Key="Rows">3</clr:Int32>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" MaxHeight="{Binding ElementName=window,Path=ActualHeight,Converter={StaticResource HeightConverter},ConverterParameter={StaticResource Rows}}">
<!-- contents ... -->
</Grid>
<Grid Grid.Row="1" MaxHeight="{Binding ElementName=window,Path=ActualHeight,Converter={StaticResource HeightConverter},ConverterParameter={StaticResource Rows}}">
<!-- contents ... -->
</Grid>
<Grid Grid.Row="2">
<!-- contents ... -->
</Grid>
</Grid>
</Window>
这里我使用了“HeightConverter”,它应该将容器的实际高度转换为您想要的高度(即将其除以3)。 (你也可以使用像MaxHeight=100
这样的固定金额,如果这对你有用。)转换器应该是这样的:
public class HeightConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double? containerHeight = (value as double?);
int numberOfRows = (parameter as int?) ?? 1;
var contentHeight = (containerHeight.Value / numberOfRows);
return contentHeight;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}