【问题标题】:Dictionary<string, List<int>> to WPF GridViewDictionary<string, List<int>> 到 WPF GridView
【发布时间】:2010-06-28 15:23:32
【问题描述】:

我想在ListView 中将Dictionary&lt;string, List&lt;int&gt;&gt; 绑定到GridView,但运气不佳。

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

但我不确定如何获取他们的代码并让它为我工作。

这是我尝试过的:

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

如何让列表显示在窗口中的相应标题下方?

【问题讨论】:

    标签: c# wpf gridview dictionary .net-3.5


    【解决方案1】:

    如果您可以使用 XAML 而不是隐藏代码,请查看以下示例:

    Problem with MVVM. Dynamic list of grids

    【讨论】:

    • 谢谢,这让我找到了一些方法。我看看能不能把它放到 GridView 中。
    【解决方案2】:

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

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

    【讨论】:

    • 更好的方法取决于您的要求。是否需要使用 XML,否则我会创建一个这样的集合:List>。您的数据是动态的,例如列数还是单个数据?这就是你在后面的代码中进行绑定的原因吗?
    • 是的,字典中的列表数量不同,每个列表中的项目数量也不同。使用 XML 不是必需的,它只是迄今为止我想出的使输出看起来像我想要的唯一方法。
    猜你喜欢
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多