将ViewModel与DataGrid一起使用

时间:2018-03-09 08:10:47

标签: c# wpf xaml mvvm datagrid

我已经创建了一个ViewModel,我想在我的DataGrid中使用它。 我已经尝试了很多东西,但我似乎无法完成它。 几天前我发布了一个关于它的问题,但我想我错了。

一些背景资料: 我有一个应用程序,其行为很像一个简单的Excel版本。 文档(表)后面的数据存储在xml文件中。 每个文档可以具有不同数量的列,列名称和行。 您可以在下面看到文档外观的示例。

<Data>
    <Row>
        <ColumnName1 ID="1000">Text in first cell of the first Column</ColumnName1>
        <ColumnName2 ID="1001">Text in first cell of the second Column</ColumnName2>
    </Row>
    <Row>
        <ColumnName1 ID="1002">Text in second cell of the first Column</ColumnName1>
        <ColumnName2 ID="1003">Text in second cell of the second Column</ColumnName2>
    </Row>
</Data>

然后我将编写一些代码来读取(可能反序列化)xml并将数据存储在ViewModel中,但首先我想确保ViewModel以我想要的方式使用我的DataGrid。 / p>

所以..下面的代码代表我的ViewModel类和一个带有一些测试数据的ViewModel对象(为了澄清一点)。

视图模型:

public class DocumentViewModel : IDocumentViewModel 
{
    int _assetID;
    string _documentName
    Table _table

    public DocumentViewModel(int assetID, string documentName, Table table)
    {
        _assetID = assetID;
        _documentName = documentName;
        _table = table;
    }

    public int AssetID { get { return _assetID; } }

    public string DocumentName { get { return _documentName; } }

    public Table Table
    {
        get { return _table }
        set 
        { 
            if (value != null) 
            { 
                _table = value 
            } 
        }
    }

}

public class Table
{
    ObservableCollection<Column> _columns;
    ObservableCollection<Row> _rows;

    public Table()
    {
        _columns = new ObservableCollection<Column>();
        _rows = new ObservableCollection<Row>();
    }

    public ObservableCollection<Column> Columns
    {
        get { return _columns; }
        set { _columns = value; }
    }

    public ObservableCollection<Row> Rows
    {
        get { return _rows; }
        set { _rows = value; }
    }
}

public class Column
{
    public string Header { get; set; }
    public int Index { get; set; }
    //public int ColumnWidth { get; set; }

}

public class Row
{
    public ObservableCollection<Cell> Cells;
}

public class Cell
{
    public int ID { get; set; }
    public string Value { get; set; }
    public bool HighLight { get; set; } //ignore highlight for now
    public bool Bold { get; set; }
    public string ForeGroundColor { get; set; }
    public string BackGroundColor { get; set; }
}

测试数据

public DocumentViewModel LoadDocument(int assetID)
        {
            //DUMMY TESTDATA BELOW
            string documentName = "TestDocument";
            ObservableCollection<Column> colList = new ObservableCollection<Column>();
            colList.Add(new Column() { Header = "Test_Header_1", Index = 0 });
            colList.Add(new Column() { Header = "Test_Header_2", Index = 1 });
            colList.Add(new Column() { Header = "Test_Header_3", Index = 2 });

            ObservableCollection<Row> rowList = new ObservableCollection<Row>();

            ObservableCollection<Cell> cellList1 = new ObservableCollection<Cell>();
            cellList1.Add(new Cell() { ID = 1, Value = "TestValue1", ForeGroundColor = "red", BackGroundColor = "yellow", Bold = false });
            cellList1.Add(new Cell() { ID = 2, Value = "TestValue2", ForeGroundColor = "orange", BackGroundColor = "blue", Bold = false });
            cellList1.Add(new Cell() { ID = 3, Value = "TestValue3", ForeGroundColor = "orange", BackGroundColor = "blue", Bold = false });

            ObservableCollection<Cell> cellList2 = new ObservableCollection<Cell>();
            cellList2.Add(new Cell() { ID = 4, Value = "TestValue4", ForeGroundColor = "red", BackGroundColor = "yellow", Bold = false });
            cellList2.Add(new Cell() { ID = 5, Value = "TestValue5", ForeGroundColor = "orange", BackGroundColor = "blue", Bold = false });
            cellList2.Add(new Cell() { ID = 6, Value = "TestValue6", ForeGroundColor = "orange", BackGroundColor = "blue", Bold = false });

            ObservableCollection<Cell> cellList3 = new ObservableCollection<Cell>();
            cellList3.Add(new Cell() { ID = 7, Value = "TestValue7", ForeGroundColor = "red", BackGroundColor = "yellow", Bold = false });
            cellList3.Add(new Cell() { ID = 8, Value = "TestValue8", ForeGroundColor = "orange", BackGroundColor = "blue", Bold = false });
            cellList3.Add(new Cell() { ID = 9, Value = "TestValue9", ForeGroundColor = "orange", BackGroundColor = "blue", Bold = false });

            rowList.Add(new Row() { Cells = cellList1 });
            rowList.Add(new Row() { Cells = cellList2 });
            rowList.Add(new Row() { Cells = cellList3 });

            Table table = new Table() { Columns = colList, Rows = rowList };

            return new DocumentViewModel(assetID, documentName, table);
        }

我想要实现的是在其中创建一个带有DataGrid的UserControl。

然后将DocumentViewModel用于UserControl中的DataContext。

然后将DocumentViewModel列和行绑定到DataGrid。

我如何实现这一目标?

0 个答案:

没有答案