C# 字典<;字符串,列表<;int>&燃气轮机;到WPF GridView
我想将C# 字典<;字符串,列表<;int>&燃气轮机;到WPF GridView,c#,wpf,gridview,dictionary,.net-3.5,C#,Wpf,Gridview,Dictionary,.net 3.5,我想将词典绑定到列表视图中的网格视图,我运气不太好 我看了这个问题:,还有这个问题: 但我不知道如何使用他们的代码并让它为我工作 以下是我尝试过的: XAML: C#: 公共部分类窗口1:窗口 { 私有字典列表=新字典(); 公共窗口1() { 初始化组件(); 列表[“a”]=新列表(新int[]{1,2,3,4,5}); lists[“b”]=新列表(新int[]{6,7,8,9}); lists[“c”]=新列表(新int[]{1,2,3,4,5}); GridView gv=新的Gr
词典
绑定到列表视图中的网格视图
,我运气不太好
我看了这个问题:,还有这个问题:
但我不知道如何使用他们的代码并让它为我工作
以下是我尝试过的:
XAML:
C#:
公共部分类窗口1:窗口
{
私有字典列表=新字典();
公共窗口1()
{
初始化组件();
列表[“a”]=新列表(新int[]{1,2,3,4,5});
lists[“b”]=新列表(新int[]{6,7,8,9});
lists[“c”]=新列表(新int[]{1,2,3,4,5});
GridView gv=新的GridView();
foreach(lists.Keys中的字符串k)
{
绑定绑定=新绑定(“[”+k+“]);
gv.Columns.Add(新的GridViewColumn
{
DisplayMemberBinding=绑定,
标题=k,
宽度=100
});
}
lv.ItemsSource=列表;
lv.视图=gv;
}
}
如何让列表显示在窗口中各自标题下方?如果您同意使用XAML而不是代码隐藏,请查看以下示例:
如果您同意使用XAML而不是代码隐藏,请查看以下示例:
我想出了一个解决方案,但我希望有更好的办法:
xaml:
c#:
公共部分类窗口1:窗口
{
受保护的字典列表=新字典();
公共窗口1()
{
初始化组件();
列表[“a”]=新列表(新int[]{1,2,3,4,5});
lists[“b”]=新列表(新int[]{6,7,8,9});
lists[“c”]=新列表(新int[]{1,2,3,4,5});
XDocument data=新XDocument();
添加(新元素(“行”);
int maxListLength=lists.Max(l=>l.Value.Count);
for(int index=0;index
我想出了一个解决方案,但我希望有更好的方法:
xaml:
c#:
公共部分类窗口1:窗口
{
受保护的字典列表=新字典();
公共窗口1()
{
初始化组件();
列表[“a”]=新列表(新int[]{1,2,3,4,5});
lists[“b”]=新列表(新int[]{6,7,8,9});
lists[“c”]=新列表(新int[]{1,2,3,4,5});
XDocument data=新XDocument();
添加(新元素(“行”);
int maxListLength=lists.Max(l=>l.Value.Count);
for(int index=0;index
谢谢,这让我走了一段路。我看看能不能把它放到GridView里。谢谢,这让我走了一段路。我会看看是否可以把它放到GridView中。更好的方法取决于您的需求。是否需要使用XML,否则我将创建一个如下的集合:List。您的数据是动态的,比如列数还是单个数据?这就是为什么要在代码隐藏中进行绑定吗?是的,字典中的列表数量不同,每个列表中的项目数量也不同。使用XML并不是一项要求,它只是我到目前为止提出的使输出看起来像我想要的唯一方法。更好的方法取决于您的要求。是否需要使用XML,否则我将创建一个如下的集合:List。您的数据是动态的,比如列数还是单个数据?这就是为什么要在代码隐藏中进行绑定吗?是的,字典中的列表数量不同,每个列表中的项目数量也不同。使用XML并不是一项要求,它只是迄今为止我提出的唯一一种使输出看起来像我想要的那样的方法。
<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>
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;
}
}
<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);
}
}
}