views:

402

answers:

1

I have this ListView and I'm using a DataTemplate (as you can see) for items. How can i add column names to the ListView with this ItemTemplate definition? I cannot use that GridViewColumn definition, because this ListView uses lazy data loading, so when there are too many rows, it fetches them on demand. GridViewColumn somehow does not function with this lazy loading.

<ListView Grid.Row="3">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="grid" Background="Transparent" MinWidth="580" >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="220" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="70"/>
                            <ColumnDefinition Width="80"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock VerticalAlignment="Center" Grid.Column="0" Text="{Binding Path=Benutzer.Value.Code}"/>
                        <TextBlock VerticalAlignment="Center" Grid.Column="1" Text="{Binding Path=Nachname}"/>
                        <TextBlock VerticalAlignment="Center" Grid.Column="2" Text="{Binding Path=Vorname}"/>
                        <TextBlock VerticalAlignment="Center" Grid.Column="3">
                                <TextBlock.Text>
                                    <Binding Path="GeburtDate" StringFormat="{}{0:d}"/>
                                </TextBlock.Text>
                        </TextBlock>
                        <Button VerticalAlignment="Center" Grid.Column="4" Style="{StaticResource StyleEditButton}" Content="Öffnen..." Tag="{Binding}"  Click="OpenPersonButton_Click"/>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListView>
+1  A: 

I don't think there is any point in using a ListView if, as you say, you cannot use GridView (or any other view). I would exchange ListView for ItemsControl (or ListBox if you need a selectable item or any of the other things that ListBox offers).

If what you say is true, you could just use a stack panel to position a header row above your data rows:

<StackPanel>
  <!-- Header -->
  <StackPanel Orientation="Horizontal">
    <TextBlock Width="220" TextAlignment="Center" Text="Code" />
    <TextBlock Width="100" TextAlignment="Center" Text="Nachname" />
    <TextBlock Width="100" TextAlignment="Center" Text="Vorname" />
    <TextBlock Width="70"  TextAlignment="Center" Text="GeburtDate" />
  </StackPanel>
  <!-- Data rows -->
  <ItemsControl>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
          <StackPanel Orientation="Horizontal" MinWidth="580">
            <TextBlock Width="220" TextAlignment="Center" Text="{Binding Path=Benutzer.Value.Code}" />
            <TextBlock Width="100" TextAlignment="Center" Text="{Binding Path=Nachname}" />
            <TextBlock Width="100" TextAlignment="Center" Text="{Binding Path=Vorname}" />
            <TextBlock Width="70"  TextAlignment="Center" Text="{Binding Path=GeburtDate, StringFormat={0:d}}" />
            <Button VerticalAlignment="Center" Grid.Column="4" Style="{StaticResource StyleEditButton}" Content="Öffnen..." Tag="{Binding}"  Click="OpenPersonButton_Click"/>
          </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</StackPanel>

Hope that helps. You might also like to read about virtualizing panels. That might provide the kind of laziness you're talking about.

Drew Noakes