【问题标题】:WPF Toolkit - Datagrid - ComboboxColumn Binding w/DynamicResourceWPF 工具包 - 数据网格 - ComboboxColumn 绑定 w/DynamicResource
【发布时间】:2009-07-22 18:07:32
【问题描述】:
我正在实现 WPF DataGrid(对 WPF 来说非常新)。我按照教程展示了如何使用静态资源绑定ComboBoxColumn。但是,我的数据网格中的一些列的数据绑定要到运行时才能知道。
因此,我无法将它们与静态资源绑定。还有其他方法可以将ComboBoxColumns 数据绑定到DataGrid 中吗?在 ASP.NET 中,我知道我们有行数据绑定代码,我们可以在其中执行此操作并动态创建列的内容。但是,在 WPF 中,看起来一切都是通过资源完成的。
如何使用DataGrid 中的动态资源进行数据绑定?
谢谢!
【问题讨论】:
标签:
c#
wpf
datagrid
wpftoolkit
【解决方案1】:
您可以动态设置绑定。
像这样(这段代码创建网格视图列并分配动态绑定)
private void AddColumn(GridView view, Field fld)
{
GridViewColumn col = new GridViewColumn();
col.Header = fld.Label;
Binding bnd = new Binding();
switch (fld.FieldType)
{
case FieldType.DateTime:
bnd.Converter = new DateTimeToDateStringConverter();
break;
// or some other converters
}
bnd.Path = new PropertyPath(string.Format("Fields[{0}]",
_table._fields.IndexOf(fld))); // the string matches what you would use in XAML
col.DisplayMemberBinding = bnd;
view.Columns.Add(col);
}