开发者

Dictionary<string, List<int>> to WPF GridView

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.c开发者_开发百科om/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?


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

Problem with MVVM. Dynamic list of grids


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);
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜