Binding ViewModel to DataGrid

2019-03-02 13:27发布

问题:

In my application there's a screen that behaves much like excel (datagrid).

I already made this application but I wanted to make everything MVVM in Version 2..

I want to be able to bind my DocumentViewModel to my DataGrid but I haven't been able to do this correctly yet. Most importantly is the Cell class where i would like to bind every property in there to the View.

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 List<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; }
}

View:

I set a filled DocumentViewmodel to the DataContext property of my UserControl in codebehind. When i run my program it doesn't give any errors but the DataGrid remains empty (No columns or rows added)

<UserControl x:Class="ViewModel.TableViewControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ViewModel"
        xmlns:commands="clr-namespace:ViewModel.Commands"
        xmlns:viewmodels="clr-namespace:ViewModel.ViewModels"
        mc:Ignorable="d">
    <DockPanel>
        <DataGrid Name="dg_TableGrid" DataContext="{Binding Table}"><!--  -->
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.HeaderTemplate>
                        <DataTemplate>
                            <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Column}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Margin="2" Width="{Binding Width}" Foreground="{Binding ForeGroundColor}" Text="{Binding Header}" />
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ItemsControl ItemsSource="{Binding Rows}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBox Margin="2" Width="{Binding Width}" Text="{Binding Data}" />
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </DockPanel>
</UserControl>