2012-09-06 137 views
-1

我有一个List<Dictionary<string,string>>对象(每个列表对象中的Dict中的Keys都是相同的),我想将它转换为List<object>,它可以通过手动生成列绑定到Datagrid。这会使原始列表对象中的每个Dictionary<string,string>成为数据网格中的一行,并且预期绑定的对象的属性是词典中的键。如何将列表<Dictionary <string,string >>对象转换为列表<object>以绑定到Datagrid?

非常感谢,

+0

不清楚 - 您期望绑定的对象的属性是什么? – n8wrl

+0

预期绑定的对象的属性是字典中的键。谢谢 – lwconquer

+0

你是否在编译时知道所有的字典键? – Servy

回答

-1

这样做

<ListView Name="selectedPeople" 
        Width="480" 
        Height="200" 
        HorizontalAlignment="Right" 
        VerticalAlignment="Top" 
        ItemsSource="{Binding Path=Maps, 
             RelativeSource={RelativeSource AncestorType=Window}, 
             Mode=OneWay}"> 
      <ListView.View> 
       <GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Broadcast call targets"> 
        <GridViewColumn DisplayMemberBinding="{Binding Path=Key}" Header="ID" /> 
        <GridViewColumn DisplayMemberBinding="{Binding Path=Value}" Header="Description" /> 
        <GridViewColumn Header=""> 
         <GridViewColumn.CellTemplate> 
          <DataTemplate> 
           <Button Content=" X " IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" /> 
          </DataTemplate> 
         </GridViewColumn.CellTemplate> 
        </GridViewColumn> 
       </GridView> 
      </ListView.View> 
     </ListView> 





    public partial class Listviewsamples : Window 
    { 
     public Listviewsamples() 
     { 
      InitializeComponent(); 

      Maps = new ObservableCollection<Dictionary<string, string>>(); 

      for (int i = 0; i < 10; i++) 
      { 
       Dictionary<string, string> item = new Dictionary<string, string>(); 
       item.Add(i.ToString(), " Item " + i.ToString()); 
       Maps.Add(item); 
      } 
      this.DataContext = this; 
     } 
     public ObservableCollection<Dictionary<string, string>> Maps { get; set; } 


    } 
+0

什么是Listviewsamples继承的窗口?它是一个用户控件吗? – lwconquer

+0

它的非用户控件的widnow类型与winforms中的Form类似。 – JSJ

-1

我觉得你的最简单的方法是创建一个表,并填充它这样的: 假设命名列表lst

DataTable table = new DataTable(); 

foreach (var col in lst[0].Keys) 
{ 
    table.Columns.Add(col, typeof(string)); 
} 

foreach (var dict in lst) 
{ 
    var row = table.NewRow(); 

    foreach (var data in dic) 
    { 
     row[data.Key] = data.Value; 
    } 

    table.Rows.Add(row); 
} 

而现在所有剩下的事情是b将该表格添加到DataGrid中!

相关问题