我创建了viewModel CustomerViewModel ,如下所示。
public class CustomerViewModel
{
public CustomerViewModel() {
customerList = null;
_repository = new CustomersRepository();
}
public List<Customer> customerList;
private CustomersRepository _repository;
public void getAllCustomers() {
customerList = _repository.GetCustomers();
}
}
我还有 CustomerView ,它看起来像
public partial class CustomerView : UserControl
{
public CustomerView()
{
InitializeComponent();
var a = new ZzaDashboard.ViewModels.CustomerViewModel();
a.getAllCustomers();
this.DataContext = a;
}
}
和CustomerView的XAML肯定
<UserControl x:Class="ZzaDashboard.Views.CustomerView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ZzaDashboard.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<DataGrid x:Name="DataGrid" AutoGenerateColumns="True" ItemsSource="{Binding customerList}"/>
</Grid>
在MainWindow.xaml中我只有这个
<Grid>
<views:CustomerView x:Name="DataGrid"/>
</Grid>
Debug显示在CustomerView.xaml.cs中,DataContext有一个 customerList ,它不是空的,但在运行我的程序后,mainWindow上没有任何显示。
这是我的第一个问题,我很抱歉不清楚我猜,我会改进。希望有人能提供帮助。