【发布时间】:2017-11-21 20:03:58
【问题描述】:
所以我有一种情况,我收到一个 Dictionary(string, string) 条目的集合,其中每个条目的键是列名和值,嗯,值。我想将这些推送到 RadDataGrid 以便每个字典映射到一行。如果我知道我会提前获得什么/多少列,我只需将它们映射到一个对象并完成它。不幸的是,它可能每次都不一样,所以这行不通。
到目前为止,我没有运气。我已经尝试直接映射它*(集合),将其转换为动态对象和 XMLDocument,但这些都不起作用。也刚刚获得了秋季创作者更新并尝试将其映射到数据表,也没有运气。
在手动添加列之后,我一直在尝试将 DataTable 的 DefaultView 映射到网格的 ItemsSource,但是虽然我得到了正确的列和标题#,但我仍然没有得到字段值。不知道下一步该去哪里。
请注意,我没有与 Telerik 结婚。如果其他人知道可以让我像这样映射任意数据的合适可用的 UWP 数据网格解决方案,我很想听听。
使用标准 UWP 应用的示例:
MainPage.xaml:
<Page xmlns:my="using:Telerik.UI.Xaml.Controls.Grid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestTelerikDataGrid"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ComponentModel="using:System.ComponentModel"
x:Class="TestTelerikDataGrid.MainPage"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="0,0,0,125">
<my:RadDataGrid Margin="0,0,0,-125" x:Name="dataGrid" AutoGenerateColumns="False" >
</my:RadDataGrid>
</Grid>
</Page>
还有后端:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace TestTelerikDataGrid {
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page {
private ObservableCollection<Dictionary<string, string>> items = new ObservableCollection<Dictionary<string, string>>();
public ObservableCollection<Dictionary<string, string>> ItemDictionary {
get {
return items;
}
set {
items = value;
}
}
public DataTable Items { get; set; }
public MainPage() {
this.InitializeComponent();
CreateItems(); // creates sample data structurally identical to what we'll get in the actual app (i.e., obsv. collection of dictionaries)
CreateTable(); // attempt to take the collection created above and map it to the RadDataGrid
}
private void CreateItems() {
for (int i = 0; i < 5; i++) {
Dictionary<string, string> row = new Dictionary<string, string>();
row["A"] = "A" + i.ToString();
row["B"] = "B" + i.ToString();
row["C"] = "C" + i.ToString();
ItemDictionary.Add(row);
}
}
private void CreateTable() {
Items = new DataTable();
if (ItemDictionary.Count == 0) return;
foreach (KeyValuePair<string, string> entry in ItemDictionary[0]) {
DataColumn column = new DataColumn(entry.Key);
Items.Columns.Add(column);
Telerik.UI.Xaml.Controls.Grid.DataGridTextColumn dgc = new Telerik.UI.Xaml.Controls.Grid.DataGridTextColumn();
dgc.Name = entry.Key;
dgc.Header = entry.Key;
dgc.PropertyName = entry.Key;
dataGrid.Columns.Add(dgc);
}
foreach (Dictionary<string, string> rowEntry in ItemDictionary) {
DataRow row = Items.NewRow();
int col = 0;
foreach (KeyValuePair<string, string> entry in rowEntry) {
row[entry.Key] = entry.Value;
}
Items.Rows.Add(row);
}
DataView dv = Items.DefaultView;
dataGrid.ItemsSource = dv;
}
}
}
理想情况下,这将产生一个包含 5 行、3 列(A、B、C)和显示正确值的字段(例如,第一行读取 A0、B0、C0)的表。
【问题讨论】:
-
看了你的长篇大论,还是不知道你做了什么?你能提供一个minimal reproducible example吗?
-
DataTable、DataColumn、DataRow、DataView是什么?您是否为您的项目安装了其他库? -
这些都是秋季创作者更新随附的 UWP 2.0 的一部分。我相信它是构建 16299 或类似的东西。抱歉,应该提到的是我正在使用的构建。