Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-level header GridView WPF

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

like image 449
michele Avatar asked Jan 28 '11 11:01

michele


1 Answers

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:

Screenshot

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;
like image 104
RQDQ Avatar answered Nov 15 '22 05:11

RQDQ