Multi-level header GridView WPF

2019-03-27 07:48发布

问题:

I need to set the listview view to a gridview that have a complex header like this (based on a 3 dimensional object list i created):

| ---------- LEVEL 0 ------------  |
| -- Level 1a --  | -- Level 1b -- |  
| Lvl A |  Lvl B  | Lvl A  | Lvl B |

EDIT: This is more ore less my object model

public class Measures
{
    public string Caption { get; set; }
    public List<Threshold> ThresholdList { get; set; }
}

public class Threshold
{
    public string Caption { get; set; }

    public double Value1 { get; set; }
    public double Value2 { get; set; }
    public double Value3 { get; set; }
    public double Value4 { get; set; }
}

i've to bind a dynamic list of Measures (this is my level 0), then a dynamic list of Threshold (level 1a...) and for each threshold display values1 to 4 if they are != 0

回答1:

How about something like this:

<ListBox x:Name="MainListBox">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <Label Content="{Binding Path=Caption}" HorizontalAlignment="Center" />

                        <ListBox Grid.Row="1" ItemsSource="{Binding Path=ThresholdList}" >
                            <ListBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                                    </StackPanel>
                                </ItemsPanelTemplate>
                            </ListBox.ItemsPanel>

                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>

                                        <Label Content="{Binding Path=Caption}" HorizontalAlignment="Center" />

                                        <StackPanel Grid.Row="1" Orientation="Horizontal">
                                            <Label Content="{Binding Path=Value1}" />
                                            <Label Content="{Binding Path=Value2}" />
                                            <Label Content="{Binding Path=Value3}" />
                                            <Label Content="{Binding Path=Value4}" />
                                        </StackPanel>
                                    </Grid>
                                </DataTemplate>
                            </ListBox.ItemTemplate>


                        </ListBox>

                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

You might need to convert the Value1, Value2 properties into a collection in order to dynamically display the non-zero ones and use the same ListBox/StackPanel approach that I used to display the thresholds.

This gives the output:

And just to complete the post, here is the code I used:

List<Measures> measuresList = new List<Measures>();

Measures measures = new Measures()
{
    Caption = "LEVEL 0",
    ThresholdList = new List<Threshold>()
};

measures.ThresholdList.Add(new Threshold()
{
    Caption = "Level 1a",
    Value1 = 1,
    Value2 = 2,
    Value3 = 3,
    Value4 = 4
});

measures.ThresholdList.Add(new Threshold()
{
    Caption = "Level 1b",
    Value1 = 5,
    Value2 = 6,
    Value3 = 7,
    Value4 = 8
});

measuresList.Add(measures);

this.MainListBox.ItemsSource = measuresList;


回答2:

Level 0 Is a ListView? Level 1A and level 1b are Gridviews that you want inside two different templated columns inside the level 0 List view? Then level A and level b are two more grid views inside 2 more templated columns in level 1a and level 1b?

You might be able to convert the same concept this nested GridView uses into WPF

http://forums.asp.net/t/1071521.aspx

I haven't tried it myself in wpf, but gridviews appeared to be built similarly. Just use panels instead of Divs, and less head aches with having to wory about client/serverside calls.

Also Datasets work nicer then lists.

Here is some psuedo code on how I've set up Nested Datasets before

DataSet ds1 = new DataSet();
        ds1.Tables.Add(m_DataLayer.GetRealA_Data(X, Y).Copy()); Returns Table ["A"]
        ds1.Tables.Add(m_DataLayer.GetB_Empty().Copy());// Returns Table ["B"]
        ds1.Tables.Add(m_DataLayer.GetC_Empty().Copy());// Returns Table ["C"]
        ds1.Tables.Add(m_DataLayer.GetD_Empty().Copy());// Returns Table ["D"]

        ds1.Relations.Add("b", ds1.Tables["A"].Columns["A_id"], ds1.Tables["B"].Columns["A_id"]);
        ds1.Relations.Add("c", ds1.Tables["B"].Columns["B_id"]
            , ds1.Tables["C"].Columns["B_id"]);
        ds1.Relations.Add("d", ds1.Tables["C"].Columns["C_id"]
            , ds1.Tables["D"].Columns["D_id"]);

        dt_TheGridViewDataSet = ds1;
        TheGridView.DataSource = ds1;


回答3:

Have you thought about how it should behave - your header that is?

It is easy enough to make a header that looks like the one you suggest - you could build it with some grids programatically - but that is just the header. Should you also be able to resize it like you normally can with a listview header?

Maybe you looking for something like a TreeListView?

http://www.codeproject.com/KB/WPF/TreeListView.aspx

I think that is what I would go after to display multidimentional data - it is easy to understand and use, where a custom listview can be hard to implement to behave right.