开发者

Add a column with fixed values to a ListView's GridView

开发者 https://www.devze.com 2023-03-15 15:31 出处:网络
I bound a ListView to a List(Of List(Of Integer)). I can specify the columns thus: <ListView ItemsSource=\"{Binding ResultsByDenomination}\">

I bound a ListView to a List(Of List(Of Integer)). I can specify the columns thus:

<ListView ItemsSource="{Binding ResultsByDenomination}">
<ListView.View>
    <GridView>
        <GridViewColumn Header="      "/>
        <GridViewColumn Header="Noord" DisplayMem开发者_StackOverflowberBinding="{Binding [0]}"/>
        <GridViewColumn Header="Oost" DisplayMemberBinding="{Binding [1]}"/>
        <GridViewColumn Header="Zuid" DisplayMemberBinding="{Binding [2]}"/>
        <GridViewColumn Header="West" DisplayMemberBinding="{Binding [3]}"/>
    </GridView>
</ListView.View>

This lays out the ListOfList neatly in a square grid. However, I want to fill the first column with fixed values. How do I do that, apart from rebuilding the ListOfListOf Integer to a ListOfList Of string and add the first column with fixed values there?

EDIT: Basically I want to add RowHeaders.


If you have one data structure for the row headers and another one for the row data, simply transform the data into a new data structure that is easily consumable by the ListView by combining them. Since you seem to like "nuts and bolts" data structures, you can use a List<Tuple<string, List<int>>> for this.

Here is a simple example. First the code-behind to simulate your ResultsByDenomination property:

void Window_Loaded(object sender, RoutedEventArgs e)
{
    var rowHeaders = new List<string> { "One", "Two" };
    var rowData = new List<List<int>>
    {
        new List<int> { 1, 2, 3, 4 },
        new List<int> { 5, 6, 7, 8 },
    };
    var listViewData = rowHeaders.Zip(rowData, (a, b) => Tuple.Create(a, b)).ToList();
    DataContext = listViewData;
}

and the modified XAML to support the new data structure:

<ListView ItemsSource="{Binding}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Item1}"/>
            <GridViewColumn Header="Noord" DisplayMemberBinding="{Binding Item2[0]}"/>
            <GridViewColumn Header="Oost" DisplayMemberBinding="{Binding Item2[1]}"/>
            <GridViewColumn Header="Zuid" DisplayMemberBinding="{Binding Item2[2]}"/>
            <GridViewColumn Header="West" DisplayMemberBinding="{Binding Item2[3]}"/>
        </GridView>
    </ListView.View>
</ListView>

You can also create a utility class instead of using Tuple with more descriptive names.

0

精彩评论

暂无评论...
验证码 换一张
取 消