I am trying to get grouping in my datagrid. I have found interesting idea on Stack Overflow, here: /a/43044022/5128696 . I sort of adapted the code to my project, here is XAML:
<UserControl.Resources >
<CollectionViewSource x:Key="persons" Source="{Binding Path=WorkersTasksGridTableSource}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="DayOfRecord" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</UserControl.Resources>
and later in same file:
<DataGrid x:Name="WorkerTasksDataGrid" AutoGenerateColumns="False" HorizontalGridLinesBrush="DarkGray" VerticalGridLinesBrush="DarkGray"
Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="6" ItemsSource="{Binding Source={StaticResource persons}}" >
<!-- /a/43044022/5128696 -->
<DataGrid.Columns>
<DataGridTextColumn Header="Start Date" Binding="{Binding Path=StartDate, StringFormat={0:dd.MM.yy HH:mm:ss}}" />
<DataGridTextColumn Header="End Date" Binding="{Binding Path=EndDate, StringFormat={0:dd.MM.yy HH:mm:ss}}" />
<DataGridTextColumn Header="Hours" Binding="{Binding Path=CalculatedTime}" />
<DataGridTextColumn Header="Project" Binding="{Binding Path=Project}" />
<DataGridTextColumn Header="Task" Binding="{Binding Path=Task}" />
<DataGridTextColumn Header="Remark" Binding="{Binding Path=Remark}" />
<DataGridTextColumn Header="Target Company, Customer, Project" Binding="{Binding Path=TargetCompany}" />
</DataGrid.Columns>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" >
<Expander.Header>
<DockPanel>
<TextBlock Text="{Binding Path=Name}" />
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
Real ItemSource Observable Collection WorkersTasksGridTableSource
is being created in my user control constructor . Groups are created finely but I get this cryptic binding error:
Severity|Count|Data Context|Binding Path |Target |Target Type |Description
Error |1 |null |NewItemMargin |DataGridRow.Margin |Thickness |Cannot find source: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1'.
I am not using NewItemMargin at all! Why am I getting this error and how to overcome such error?