Home > Software design >  Output data from loop to xaml scrollable table
Output data from loop to xaml scrollable table

Time:10-19

I have a loop like so:

foreach (KeyValuePair<string, int> kvp in Dictionary)
{
   listFirstColumn.Add(kvp.Key);    
   listSecondColumn.Add(kvp.Value);    
}

My question is how do I display data in WPF UI Framework, so that the listFirstColumn goes inside first column and listSecondColumn goes inside second.

All of which is scrollable.

What xaml tool do I use, list, grid? Also How do you output that data? I can't seem to find a straightforward resource or answer

CodePudding user response:

Go to your view model where listFirstColumn and listSecondColumn reside

using System.Linq;
using System.Collections.Generic;
...
public class Columns 
{
     public string Col1 { get; set; }
     public int Col2 { get; set; }
}
public IEnumerable<Columns> GridItems => listFirstColumn.Zip(listSecondColumn, (first, second) =>
    new Columns() { Col1 = first, Col2 = second })

If you don't want to declare a new class, check out this post: Populating DataGrid in WPF with anonymous type collection

I assume you used a code sample from GridView docs:

<ListView ItemsSource="{Binding GridItems}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Col1}" Header="Whatever" Width="100"/>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Col2}" Header="Whatever" Width="100"/>
        </GridView>
    </ListView.View>
</ListView>
  • Related