I would like to bind a Dictionary<string, List<int>>
to a GridView
in a ListView
and I'm not having much luck.
I've looked at this question: wpf-binding-dictionarystring-liststring-to-listview-listbox-how, and this question: dictionary-binding-to-listview
But I'm not sure how to take their code and make it work for me.
Here's what I've tried:
XAML:
<Window x:Class="WpfIdeas.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="315">
<Grid x:Name="theGrid">
<ListView x:Name="lv">
</ListView>
</Grid>
</Window>
C#:
public partial class Window1 : Window
{
private Dictionary<string, List<int>> lists = new Dictionary<string, List<int>>();
public Window1()
{
InitializeComponent();
lists["a"] = new List<int>(new int[] {1, 2, 3, 4, 5});
lists["b"] = new List<int>(new int[] { 6, 7, 8, 9});
lists["c"] = new List<int>(new int[] { 1, 2, 3, 4, 5 });
GridView gv = new GridView();
foreach(string k in lists.Keys)
{
Binding binding = new Binding("[" + k + "]");
gv.Columns.Add(new GridViewColumn
{
DisplayMemberBinding = binding,
Header = k,
Width = 100
});
}
lv.ItemsSource = lists;
lv.View = gv;
}
}
How can I get the lists to display in the window, below their respective headings?