0

最初我想这样做,但除了为每个组(非流动资产、流动资产等)ItemsControl使用一个之外,我想不出其他任何方法。ItemsControl为了降低我用来TreeView呈现信息的复杂性,它的外观如下:

在此处输入图像描述

不错!我现在想要的是为每个粗体头和两个组总计添加子组总计:一个用于非流动资产和流动资产,另一个用于非流动负债、流动负债和基金。这是 xaml 的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>

和数据结构:

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; }
}

可以用HeaderedItemsControl代替Treeview吗?我还没有看到任何HeaderedItemsControl可以与之合作的好例子。

4

0 回答 0