我创建了看起来像这样的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;
}
}
和xaml of Customerview肯定
<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上什么也没有显示。
这是我的第一个问题,很抱歉,我想我不清楚,我会有所改善。希望任何人都会帮助。
问题是客户名单需要属性而不是字段。感谢@ASH。