0

我试图创建一个相当复杂的窗口,其中有多个部分显示一些数据列表。现在根据来源,每个部分的列表中可以有 1 个或多个项目。如果可能,最好每个列表都缩小到数据量,但如果空间用完,则在每个 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>
4

1 回答 1

1

如果您将 RowDefinition 设置HeightAuto,那么它将扩展到尽可能多的位置以容纳行内的任何内容。因此,您需要MaxHeightRowDefinition或其内容(内部网格)上进行设置,以限制它可以扩展的数量。

如果您事先知道 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",它应该将容器的实际高度转换为您想要的高度(即,将其除以三)。(你也可以只使用一个固定的数量,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();
    }
}

例子

于 2012-09-12T00:17:49.403 回答