2012-09-12 36 views
0

我試圖創建一個相當複雜的窗口,其中有多個部分顯示一些數據列表。現在取決於來源,每個部分可以有一個或多個項目在列表中。如果可能的話,最好是每個列表縮小到數據量,但如果空間用完,則在每個ListView中顯示滾動條。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> 

回答

1

如果設置了RowDefinition的HeightAuto,然後它會擴展到儘可能多的必要,以適應該行內部的一切。因此,您需要在RowDefinition或其內容(內部網格)上設置MaxHeight來限制它可以擴展的數量。

如果您事先知道它,您可以將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(); 
    } 
} 

Example

+0

感謝您的。我希望這個空間可以更加聰明一點,就好像第一部分有很多物品,而另外兩個只有1或者0,那麼第一部分將無法擴展超過1/3的空間。 – Cheval