WPF personnalisé ListView avec Style (à l'aide de DataTemplate) - comment puis-je ajouter des en-têtes?

J'ai le code suivant

DataTemplate pour les lignes

<!-- Template for each item in ListView -->
<DataTemplate x:Key="ItemTemplate">
<Grid>                                
<Grid.ColumnDefinitions>                    
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="325"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type l:ItemsView}}, Path=ParentColumnWidth}"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>                    
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" x:Name="Statement" IsChecked="{Binding Path=statement}" Foreground="{StaticResource CustomWhite}" VerticalAlignment="Center" Style="{StaticResource SelectionCheckBox}"/>
<TextBlock Grid.Column="1" Text="{Binding Path=idate, StringFormat=d MMM yy}" FontSize="15" Foreground="{StaticResource CustomWhite}"/>
<TextBlock Grid.Column="2" Text="{Binding Path=fullcomment}" FontSize="15" Foreground="{StaticResource CustomWhite}"/>
<TextBlock Grid.Column="3" Text="{Binding Path=amount, StringFormat={}{0:N2}}" FontSize="15" Foreground="{Binding Converter={StaticResource GetColourConverterItemAmount}}" TextAlignment="Right" Padding="0,0,25,0"/>
<TextBlock Grid.Column="4" Text="{Binding Path=acc}"  FontSize="15" Foreground="{StaticResource CustomWhite}"/>
<TextBlock Grid.Column="5" Text="{Binding Path=source}" FontSize="15" Foreground="{StaticResource CustomWhite}"/>
<TextBlock Grid.Column="6" Text="{Binding Path=transfer}" FontSize="15" Foreground="{StaticResource CustomWhite}"/>                
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=statement}" Value="{x:Null}">
<Setter TargetName="Statement" Property="IsEnabled" Value="False"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>

Style pour ListView - ci-dessus en utilisant DataTemplate

<!-- ListView template -->
<Style x:Key="HistoryListView" TargetType="{x:Type ListView}">
<Setter Property="ItemTemplate" Value="{StaticResource ItemTemplate}"/>
<Setter Property="Background" Value="{StaticResource CustomBackground}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Margin" Value="10,10,10,10"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Padding" Value="0,0,50,0"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>            
<Style.Resources>
<!-- Makes selection stay when focus lost (for context menu)-->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{StaticResource CustomLightHighlightC}"/>
</Style.Resources>
</Style>        
<Style x:Key="HistoryContainerStyle" TargetType="ListViewItem">
<Setter Property="ContextMenu" Value="{StaticResource ItemMenu}"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Style>

Et ma liste est définie comme

<ListView x:Name="lstHistory" ItemsSource="{Binding Path=Items}" Style="{StaticResource HistoryListView}" MouseDoubleClick="lvShowItem" SelectionChanged="lstSelectionChanged" ItemContainerStyle="{StaticResource HistoryContainerStyle}"/>                

Ce produit une ListView qui est exactement ce que je veux, sauf que j'ai pas les en-têtes et idéalement, je voudrais sortable en-têtes. Je tiens également à ajouter les en-têtes dans le Style, si possible, afin que je puisse le réutiliser dans d'autres modules.

Toute aide appréciée
Merci
Andy

  • Selon vos besoins, je pense que DataGrid est bien meilleure option pour les utiliser ici. Vous pouvez définir IsReadOnly à True si vous ne voulez pas de fonction d'édition.
  • Je ne pense pas (mais je me trompe peut-être) je peux personnaliser l'INTERFACE utilisateur d'un DataGrid dans la façon dont je peux un ListView
InformationsquelleAutor Andy Powell | 2013-12-26