简体   繁体   English

如何将 ObservableCollection 的字典绑定到 DataGrid?

[英]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>>绑定到DataGridItemSource属性。

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:我的想法是:

  • Converter that knows how to access data知道如何访问数据的转换器
  • Mapping the data to custom-tailored classes per each table (which I really wanna avoid since it's unknown what tables will be required)将数据映射到每个表的定制类(我真的想避免这种情况,因为不知道需要哪些表)
  • Somehow binding a collection of dictionaries to the DataGrid (trying to do it but can't quite understand how to)以某种方式将字典集合绑定到 DataGrid(尝试这样做但不太明白如何做)
  • Somehow manually insert rows in a DataGrid from code-behind?以某种方式从代码隐藏中手动在 DataGrid 中插入行?

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 的评论,我似乎试图通过创建DataTableDataColumnDataRow和其他类来重新发明轮子。

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.

相关问题 如何以编程方式将WPF Datagrid绑定到自定义对象的ObservableCollection? - How do I programmatically bind a WPF Datagrid to a ObservableCollection of custom objects? 如何将ObservableCollection与datagrid WPF绑定 - How to bind ObservableCollection with datagrid WPF 如何将WPF DataGrid绑定到ObservableCollection - How to bind WPF DataGrid to ObservableCollection 如何将 wpf 数据网格粘贴到 observablecollection - How do I paste in a wpf datagrid to an observablecollection 如何将字符串的observableCollection绑定到listBox - How do I Bind observableCollection of strings to a listBox 如何将 ObservableCollection 绑定到 AvalonDock DocumentPaneGroup? - How do I bind an ObservableCollection to an AvalonDock DocumentPaneGroup? 将数据网格从 ObservableCollection 绑定到 ObservableCollection - Bind datagrid to an ObservableCollection from an ObservableCollection 如何将类型为字符串的ObservableCollection绑定到Datagrid - How to bind a ObservableCollection of type string to Datagrid 如何将“DataGrid.Items”与 ObservableCollection 绑定<sometype[]> ?</sometype[]> - How to bind “DataGrid.Items” with ObservableCollection<SomeType[]>? 如何在我的情况下使用mvvm绑定DataGrid(ObservableCollection内的ObservableCollection) - How to bind DataGrid in my situation using mvvm (ObservableCollection inside ObservableCollection)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM