[英]How do I bind an ObservableCollection of Dictionaries to DataGrid?
I'm trying to understand how can I bind an ObservableCollection<*Dictionary<string, DBRecord>>
to an ItemSource
property of a DataGrid
.我试图了解如何将
ObservableCollection<*Dictionary<string, DBRecord>>
绑定到DataGrid
的ItemSource
属性。
Below you can see the classes that represent my db table in memory.您可以在下面看到代表我在 memory 中的数据库表的类。 I need to do it this way because table schema is described in an standalone.xml file (I can't change it).
我需要这样做,因为表模式在standalone.xml 文件中描述(我无法更改它)。
public abstract class DBField {
public string Name;
public DBField(string name) {
Name = name;
}
}
public class DBValueField<T> : DBField {
public T Value;
public DBValueField(string name, T value) : base(name) {
Value = value;
}
}
public class DBTextField : DBValueField<string> {
public DBTextField(string name, string value) : base(name, value) { }
}
public class DBIntField : DBValueField<int> {
public DBIntField(string name, int value) : base(name, value)
{}
}
public class DBFloatField : DBValueField<float> {
public DBFloatField(string name, float value) : base(name, value)
{}
}
public class DBDoubleField : DBValueField<double> {
public DBDoubleField(string name, double value) : base(name, value)
{}
}
public class DBBoolField : DBValueField<bool> {
public DBBoolField(string name, bool value) : base(name, value)
{}
}
public class DBRecord {
public Dictionary<string, DBField> Fields { get; set; }
}
public class DBTable {
private readonly XmlSchema schema;
public List<DBRecord> Records { get; set; }
}
And this is my Database ViewModel:这是我的数据库视图模型:
public class DatabaseViewModel : BaseViewModel {
public Dictionary<string, DBTable> Tables { get; private set; }
public DBTable ActiveTable { get; private set; }
public ObservableCollection<DBRecord> ActiveData { get; set; }
}
I have a ComboBox with a full list of all loaded db tables and I have a DataGrid element right under that ComboBox.我有一个ComboBox ,其中包含所有加载的数据库表的完整列表,并且我在该 ComboBox 下方有一个DataGrid元素。 The idea is that when you select a particular table, the ViewModel will populate the DataGrid with columns (retrieved from table schema) and with values (values are assigned to the ActiveData as a reference and retrieved from a selected table in a ComboBox).
这个想法是,当您 select 特定表时,ViewModel 将使用列(从表模式检索)和值(值分配给ActiveData作为引用并从组合框中的选定表中检索)填充 DataGrid。 The problem is that I don't understand how to properly bind such a structure to a DataGrid.
问题是我不明白如何正确地将这样的结构绑定到 DataGrid。
My ideas were:我的想法是:
This is my WPF markup:这是我的 WPF 标记:
<ComboBox x:Name="tablesComboBox"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="8"
Style="{StaticResource DarkFlatComboBox}"
ItemsSource="{Binding Tables, Mode=OneWay}"
DisplayMemberPath="Key"
SelectedValuePath="Value"
SelectionChanged="ComboBox_SelectionChanged"/>
<DataGrid x:Name="dataGrid"
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="8"
AutoGenerateColumns="True"
LoadingRow="DataGrid_LoadingRow"
Style="{StaticResource DarkDataGrid}"
ItemsSource="{Binding ActiveTable.Records,
Mode=TwoWay}"/>
Based on BionicCode's comment it seems that I tried to reinvent the wheel by creating DataTable
, DataColumn
, DataRow
and other classes.根据 BionicCode 的评论,我似乎试图通过创建
DataTable
、 DataColumn
、 DataRow
和其他类来重新发明轮子。
I removed my classes and based my code around System.Data classes for tables.我删除了我的类,并将我的代码基于表的 System.Data 类。
Besides, DataTable
perfectly binds to a DataGrid
and it solves all my issues.此外,
DataTable
完美地绑定到DataGrid
,它解决了我所有的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.