Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is how I solved similar problem:</p> <pre><code>&lt;ListView x:Name="listViewOpportunitiesHistory" ItemsSource="{Binding}" AlternationCount="2" IsTextSearchEnabled="False" IsSynchronizedWithCurrentItem="True" &gt; &lt;ListView.GroupStyle&gt; &lt;GroupStyle&gt; &lt;GroupStyle.ContainerStyle&gt; &lt;Style TargetType="{x:Type GroupItem}"&gt; &lt;Setter Property="Margin" Value="0,0,0,5"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type GroupItem}"&gt; &lt;Grid x:Name="grid_ControlTemplate_listViewOpportunitiesHistory" Loaded="grid_ControlTemplate_listViewOpportunitiesHistory_Loaded"&gt; &lt;Expander BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1" x:Name="expanderControlTemplate" Expanded="expanderExpandCollapse_Expanded" Collapsed="expanderExpandCollapse_Collapsed" &gt; &lt;Expander.Header&gt; &lt;DockPanel&gt; &lt;TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0" Width="Auto"/&gt; &lt;TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/&gt; &lt;TextBlock FontWeight="Bold" Text=" Items"/&gt; &lt;/DockPanel&gt; &lt;/Expander.Header&gt; &lt;Expander.Content&gt; &lt;ItemsPresenter /&gt; &lt;/Expander.Content&gt; &lt;/Expander&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/GroupStyle.ContainerStyle&gt; &lt;/GroupStyle&gt; &lt;/ListView.GroupStyle&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridView.Columns&gt; &lt;GridViewColumn Width="Auto" x:Name="listViewOpportunitiesHistoryColumn_Time" &gt; &lt;GridViewColumnHeader Click="SortClickHistory" Tag="Time" Content=" Time " x:Name="listViewOpportunitiesHistoryColumnHeader_Time" /&gt; &lt;GridViewColumn.CellTemplate &gt; &lt;DataTemplate&gt; &lt;Border BorderBrush ="Gray" BorderThickness="0,0,1,0" Margin="-6,0,-6,0"&gt; &lt;Grid Margin="6,0,6,0" &gt; &lt;TextBlock Text="{Binding Path=Time, StringFormat='yyyy-MM-dd HH:mm:ss.fff'}" Grid.Column ="0" TextTrimming="CharacterEllipsis" VerticalAlignment="Center" HorizontalAlignment="Left" ToolTipService.ToolTip="{Binding Path=Time}" /&gt; &lt;/Grid&gt; &lt;/Border&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn Width="Auto" x:Name="listViewOpportunitiesHistoryColumn_AOValue" &gt; &lt;GridViewColumnHeader Click="SortClickHistory" Tag="AOValue" Content=" AO Value " x:Name="listViewOpportunitiesHistoryColumnHeader_AOValue" /&gt; &lt;GridViewColumn.CellTemplate &gt; &lt;DataTemplate&gt; &lt;Border BorderBrush ="Gray" BorderThickness="0,0,1,0" Margin="-6,0,-6,0"&gt; &lt;Grid Margin="6,0,6,0" &gt; &lt;TextBlock Text="{Binding Path=AOValue}" Grid.Column ="0" TextTrimming="CharacterEllipsis" VerticalAlignment="Center" HorizontalAlignment="Left" ToolTipService.ToolTip="{Binding Path=AOValue}" /&gt; &lt;/Grid&gt; &lt;/Border&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;/GridView.Columns&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre> <p>Code behind:</p> <pre><code>private Dictionary&lt;string, bool?&gt; expandStates = new Dictionary&lt;string, bool?&gt;(); private void grid_ControlTemplate_listViewOpportunitiesHistory_Loaded(object sender, RoutedEventArgs e) { var grid = (Grid)sender; var dc = grid.DataContext as CollectionViewGroup; var groupName = (string)dc.Name.ToString(); //If the dictionary contains the current group, retrieve a saved state of the group if (this.expandStates.ContainsKey(groupName)) { var expander = (Expander)grid.FindName("expanderControlTemplate"); //btn.IsExpanded = this.expandStates[groupName]; if (this.expandStates[groupName] == true) { expander.IsExpanded = true; } if (this.expandStates[groupName] == false) { expander.IsExpanded = false; } } } private void expanderExpandCollapse_Collapsed(object sender, RoutedEventArgs e) { var expander = (Expander)sender; var dc = (CollectionViewGroup)expander.DataContext; var groupName = (string)dc.Name.ToString(); //Loaded event is fired earlier than the Click event, so I'm sure that the dictionary contains the key this.expandStates[groupName] = expander.IsExpanded; //Save the current state } private void expanderExpandCollapse_Expanded(object sender, RoutedEventArgs e) { var expander = (Expander)sender; var dc = (CollectionViewGroup)expander.DataContext; var groupName = (string)dc.Name.ToString(); //Loaded event is fired earlier than the Click event, so I'm sure that the dictionary contains the key this.expandStates[groupName] = expander.IsExpanded; //Save the current state } </code></pre> <p>I get the data from DataSet:</p> <pre><code>ICollectionView view = CollectionViewSource.GetDefaultView(dataSet.Tables[0]); view.GroupDescriptions.Add(new PropertyGroupDescription(FXH.string_GroupBy_OpportunitiesHistory)); listViewOpportunitiesHistory.ItemsSource = view; </code></pre> <p>Just remove some unneeded parts of code. My solution is partially based on <a href="https://stackoverflow.com/questions/4986255/sort-and-group-listitems-in-a-wpf-listbox-groupitem-collapse-and-expand">Sort and Group ListItems in a WPF ListBox- GroupItem collapse and expand</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload