Initially I wanted to do it with ItemsControl
but I couldn't think of anything other than using one ItemsControl
for each group (Non Current Assets, Current Assets, etc.). To reduce the complexity I've used TreeView
instead to present the information and with that here's how it looks:
Not bad! What I want now is to add sub group total for each Bold head and two group total: one for Non Current Assets and Current Assets and another for Non Current Liabilities, Current Liabilities and Fund. Here's the xaml for the TreeView
:
<TreeView ItemsSource="{Binding BalanceSheet}"
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
BorderThickness="0">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ContentPresenter ContentSource="Header"
Grid.ColumnSpan="2"/>
<ItemsPresenter Grid.Row="1" Grid.Column="1"
Name="ItemsHost"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Ledgers}">
<TextBlock Text="{Binding Book}" FontWeight="Bold" FontSize="14"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding Ledger}"/>
<TextBlock HorizontalAlignment="Right"
Text="{Binding Amount, StringFormat='N0'}"/>
</DockPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
and the Data Structures:
List<Balances> balanceSheet;
public List<Balances> BalanceSheet { get => balanceSheet; set { balanceSheet = value; OnPropertyChanged(); } }
public class Balances
{
public string Book { get; set; }
public List<Balance> Ledgers { get; set; }
public Balances()
{
Ledgers = new List<Balance>();
}
}
public class Balance
{
public string Book { get; set; }
public string Ledger { get; set; }
public int Amount { get; set; }
}
Is it possible to use HeaderedItemsControl
instead of Treeview
? I haven't seen any good example of HeaderedItemsControl
to work with.