代码之家  ›  专栏  ›  技术社区  ›  Matt Ellen Bipin Vayalu

字典<string,列表<int>>到WPF GridView

  •  1
  • Matt Ellen Bipin Vayalu  · 技术社区  · 16 年前

    我想订一张 Dictionary<string, List<int>> GridView 在一个 ListView 我运气不太好。

    我看过这个问题: wpf-binding-dictionarystring-liststring-to-listview-listbox-how dictionary-binding-to-listview

    但我不知道如何接受他们的代码,让它为我工作。

    以下是我尝试过的:

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

    如何让列表显示在窗口中,在它们各自的标题下面?

    2 回复  |  直到 9 年前
        2
  •  0
  •   Matt Ellen Bipin Vayalu    16 年前

    我想出了一个解决办法,但我希望有更好的办法:

    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>
    

    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);
                }
            }
        }
    
    推荐文章