views:

361

answers:

2

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?

+1  A: 

If you are OK with using XAML instead of code behind, check out this example:

http://stackoverflow.com/questions/3117179/problem-with-mvvm-dynamic-list-of-grids/3118950#3118950

Wallstreet Programmer
Thanks, this has got me some of the way there. I'll see if I can put this into a GridView.
Matt Ellen
A: 

I have come up with a solution, but I'm hoping there is a better way:

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">
    <Window.Resources>
        <XmlDataProvider x:Key="xdp"></XmlDataProvider>
    </Window.Resources>
    <Grid x:Name="theGrid">
        <ListView x:Name="lv" ItemsSource="{Binding Source={StaticResource xdp}, XPath=/rows/row}">
            <ListView.View>
                <GridView x:Name="gv">
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

c#:

public partial class Window1 : Window
    {
        protected 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});

            XDocument data = new XDocument();
            data.Add(new XElement("rows"));

            int maxListLength = lists.Max(l => l.Value.Count);

            for (int index = 0; index < maxListLength; ++index )
            {
                XElement row = new XElement("row");
                foreach(string k in lists.Keys)
                {
                    XElement value = new XElement(k);
                    if(index < lists[k].Count)
                    {
                        value.Value = lists[k][index].ToString();
                    }
                    row.Add(value);
                }
                data.Root.Add(row);
            }

            (Resources["xdp"] as XmlDataProvider).Document = new XmlDocument{InnerXml = data.ToString()};

            foreach (string key in lists.Keys)
            {
                GridViewColumn gvc = new GridViewColumn();
                gvc.Header = key;
                gvc.Width = 75;
                gvc.DisplayMemberBinding = new Binding { XPath = key };
                gv.Columns.Add(gvc);
            }
        }
    }
Matt Ellen
Better way depends on your requirements. Is using XML a requirement, otherwise I would create a collection like this: List<Dictionary<string, int?>>. Is your data dynamic, like number of columns or individual data? Is that why you do the binding in code behind?
Wallstreet Programmer
Yes, the number of lists in the dictionary varies, as does the number of items in each list. Using XML is not a requirement, it's just the only way I've come up with so far of making the output look like I want.
Matt Ellen