This solution is not very great, and it is a hack-ish, but it will basically do what you want. I made the listview headers invisible, placed a text block above the list, and set the text value to the groupitem of the first visible item in the list. Hacky, but this is the best I've come up with.
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); List<String> colList1 = new List<string>() { "Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7" }; List<String> colList2 = new List<string>() { "1", "2", "3", "4", "5", "6" }; ObservableCollection<Data> dataCollection = new ObservableCollection<Data>(); for (var a = 0; a < 100; a++) { Random rnd = new Random(); int min = rnd.Next(5000); int rnd1 = rnd.Next(0, 6); int rnd2 = rnd.Next(0, 5); dataCollection.Add( new Data() { Date = DateTime.Now.AddMinutes(min).ToString("hh:MM tt"), Col1 = colList1[rnd2], Col2 = String.Format("Col2: {0}", "X"), Col3 = colList2[rnd2] } ); } this.DataContext = dataCollection; } public class Data { public string Date { get; set; } public string Col1 { get; set; } public string Col2 { get; set; } public string Col3 { get; set; } } private void grid_ScrollChanged(object sender, ScrollChangedEventArgs e) { if (grid.Items.Count > 0) { HitTestResult hitTest = VisualTreeHelper.HitTest(grid, new Point(5, 5)); System.Windows.Controls.ListViewItem item = GetListViewItemFromEvent(null, hitTest.VisualHit) as System.Windows.Controls.ListViewItem; if (item != null) Head.Text = ((Data)item.Content).Date; } } System.Windows.Controls.ListViewItem GetListViewItemFromEvent(object sender, object originalSource) { DependencyObject depObj = originalSource as DependencyObject; if (depObj != null) {
XAML:
<Window x:Class="header.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="auto" SizeToContent="Width"> <Window.Resources> <CollectionViewSource x:Key="data" Source="{Binding}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="Date"/> </CollectionViewSource.GroupDescriptions> </CollectionViewSource> <Style x:Key="myHeaderStyle" TargetType="{x:Type GridViewColumnHeader}"> <Setter Property="Visibility" Value="Collapsed" /> </Style> </Window.Resources> <Grid> <StackPanel> <TextBlock Name="Head" Grid.Row="0"/> <ListView Name="grid" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding Source={StaticResource data}}" Height="300" ScrollViewer.ScrollChanged="grid_ScrollChanged"> <ListView.View> <GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}"> <GridView.Columns> <GridViewColumn DisplayMemberBinding="{Binding Col1}" Width="100"/> <GridViewColumn DisplayMemberBinding="{Binding Col2}" Width="100"/> <GridViewColumn DisplayMemberBinding="{Binding Col3}" Width="100"/> </GridView.Columns> </GridView> </ListView.View> <ListView.GroupStyle> <GroupStyle> <GroupStyle.ContainerStyle> <Style TargetType="{x:Type GroupItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type GroupItem}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid Grid.Row="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <TextBlock Background="Beige" FontWeight="Bold" Text="{Binding Path=Name, StringFormat={}{0}}"/> </Grid> <DockPanel Grid.Row="1"> <ItemsPresenter Grid.Row="2"></ItemsPresenter> </DockPanel> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </GroupStyle.ContainerStyle> </GroupStyle> </ListView.GroupStyle> </ListView> </StackPanel> </Grid>
EDIT: Fixed ScrollChanged event.
private void grid_ScrollChanged(object sender, ScrollChangedEventArgs e) { if (grid.Items.Count > 0) { Point point = new Point(5, 5); foreach(Data lvItem in grid.Items) { HitTestResult hitTest = VisualTreeHelper.HitTest(grid, point); ListViewItem item = GetListViewItemFromEvent(null, hitTest.VisualHit) as System.Windows.Controls.ListViewItem; if (item != null) { Data value = ((Data)item.Content); Head.Text = ((Data)item.Content).Date; break; } else { point.X += 5; point.Y += 5; } } } }
Tomcat
source share