如何/可以/可能将WPF DataGrid绑定到一个对象列表,每个对象在字典中都有一些值<;字符串,字符串>;?
风滚草的主要编辑,可能仍然是风滚草 如果我有一个客户列表,并且每个客户的数据都包含在字典中,那么如何将列表绑定到DataGrid,以便每个字符串键都是一列 编辑:注意,我知道这不是设计客户类的好方法 e、 g如何/可以/可能将WPF DataGrid绑定到一个对象列表,每个对象在字典中都有一些值<;字符串,字符串>;?,wpf,wpftoolkit,Wpf,Wpftoolkit,风滚草的主要编辑,可能仍然是风滚草 如果我有一个客户列表,并且每个客户的数据都包含在字典中,那么如何将列表绑定到DataGrid,以便每个字符串键都是一列 编辑:注意,我知道这不是设计客户类的好方法 e、 g 我不了解你的客户类别的设计。更安全的方法是如下所示的客户类。这也会使绑定更容易 public class Customer { public int Id { get; set; } public string Name { get; set; } public
我不了解你的客户类别的设计。更安全的方法是如下所示的客户类。这也会使绑定更容易
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public HairColor HairColor { get; set; }
}
public enum HairColor
{
SeeThrough,
Black,
Brown,
Blond
}
如果原因是因为这是你从数据库中得到的,那么把你的类看作是模型和我的类作为VIEW模型,在VIEW模型类中做适当的转换。在没有其他建议的情况下,
< P>是我能想出的最好的答案。在他的文章中,Miron正在处理来自web服务的XML数据,并将其转换为反射生成的数据绑定类型 我可以按照他的方法创建一个使用字典键而不是xml节点作为生成类型属性源的类型。对于我正在构建的其他简单应用程序来说,它似乎相当强大,但至少我了解了反射API 如果有人愿意评论,或者仍然能为我提供更好的解决方案,我将不胜感激或者,简单的道路
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
var a1 = new A();
a1["Name"] = "Jack";
a1["Age"] = "9";
var a2 = new A();
a2["Name"] = "Jill";
a2["Age"] = "7";
List<A> items = new List<A>() { a1, a2 };
this.DataBoundItems = items;
dg.DataContext = this;
}
public List<A> DataBoundItems { get; set; }
private void dg_DataContextChanged(
object sender,
DependencyPropertyChangedEventArgs e)
{
foreach (string key in DataBoundItems[0].Values.Keys)
{
var col = new DataGridTextColumn();
col.Header = key;
// bind to the indexer on the class
col.Binding = new Binding("[" + key + "]");
dg.Columns.Add(col);
}
}
}
public class A
{
private Dictionary<string, string> values = new Dictionary<string, string>();
public string this[string index]
{
get
{
return values[index];
}
set
{
values[index] = value;
}
}
public Dictionary<string, string> Values { get { return aValues; } }
}
公共部分类窗口2:窗口
{
公共窗口2()
{
初始化组件();
var a1=新的A();
a1[“名称”]=“插孔”;
a1[“年龄”]=“9”;
var a2=新的A();
a2[“姓名”]=“吉尔”;
a2[“年龄”]=“7岁”;
列表项=新列表(){a1,a2};
this.DataBoundItems=项目;
dg.DataContext=this;
}
公共列表DataBoundItems{get;set;}
私有无效dg_DataContextChanged(
对象发送器,
DependencyPropertyChangedEventArgs(附件e)
{
foreach(DataBoundItems[0]中的字符串键。值。键)
{
var col=新的DataGridTextColumn();
列头=键;
//绑定到类上的索引器
列绑定=新绑定(“[”+键+“]”);
dg.列。添加(列);
}
}
}
公共A类
{
私有字典值=新字典();
公共字符串此[字符串索引]
{
得到
{
返回值[索引];
}
设置
{
数值[指数]=数值;
}
}
公共字典值{get{return aValues;}}
}
我更愿意使用一种安全类型的方法,不幸的是,我正在研究的是,我必须考虑从数据源(Excel扩展表)中显示字段,在那里我不能准确地预测客户中包含了多少字段。我的客户希望它具有灵活性。目前正在看这篇文章,它描述如何生成一个类型来创建对象,以表示从web服务接收的xml数据。不过似乎有点重。
<DataGrid ItemsSource={Binding Path=Customers}/>
Id | Name | Age | HairColour
________________________
1 | Peter| 129 | See through!
________________________
2 | Peter| 129 | See through!
________________________
3 | Peter| 129 | See through!
________________________
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public HairColor HairColor { get; set; }
}
public enum HairColor
{
SeeThrough,
Black,
Brown,
Blond
}
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
var a1 = new A();
a1["Name"] = "Jack";
a1["Age"] = "9";
var a2 = new A();
a2["Name"] = "Jill";
a2["Age"] = "7";
List<A> items = new List<A>() { a1, a2 };
this.DataBoundItems = items;
dg.DataContext = this;
}
public List<A> DataBoundItems { get; set; }
private void dg_DataContextChanged(
object sender,
DependencyPropertyChangedEventArgs e)
{
foreach (string key in DataBoundItems[0].Values.Keys)
{
var col = new DataGridTextColumn();
col.Header = key;
// bind to the indexer on the class
col.Binding = new Binding("[" + key + "]");
dg.Columns.Add(col);
}
}
}
public class A
{
private Dictionary<string, string> values = new Dictionary<string, string>();
public string this[string index]
{
get
{
return values[index];
}
set
{
values[index] = value;
}
}
public Dictionary<string, string> Values { get { return aValues; } }
}