在我的应用程序中,有一个与excel(datagrid)非常相似的屏幕。
我已经制作了这个应用程序,但我想把MVVM全部打造成 版本2 ..
我希望能够将DocumentViewModel绑定到我的DataGrid,但我还没能正确地执行此操作。 最重要的是Cell类,我希望将其中的每个属性绑定到View。
视图模型:
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; }
}
查看:
我在一个代码隐藏中将一个填充的DocumentViewmodel设置为UserControl的DataContext属性。 当我运行我的程序时,它不会给出任何错误,但DataGrid保持为空(没有添加列或行)
<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>