我有一个List<Dictionary<string,string>>
对象(每个列表对象中的Dict中的键是相同的),我想将它转换为List<object>
,可以手动绑定到Datagrid生成列。这将使原始列表对象中的每个Dictionary<string,string>
成为数据网格中的一行,而预期要绑定的对象上的属性是字典中的键。
非常感谢,
卫
答案 0 :(得分:-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; }
}
答案 1 :(得分:-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);
}
现在要做的就是将该表绑定到DataGrid!