我想出了一个解决办法,但我希望有更好的办法:
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);
}
}
}