【发布时间】:2021-03-10 17:28:59
【问题描述】:
作为 WPF 和 MVVM 的新手,过去几天我一直在努力解决这个问题。我已经在 stackoverflow 和 google/Youtube 上搜索了所有的帮助。
我有一个从模型的 BindableCollection (Caliburn Micro) 填充的 DataGrid(来自 OrderListView 的 biound)。但是,我需要从另一个 BindableCollection ProductList 中引入链接数据的属性(“程序”),(两个集合共享一个公共属性“代码”。
基本上,我希望 DataGrid 显示所有基于 OrderModel 的列,并在运行时使用 Products 集合中的相关数据填充名为 Programs 的列。
OrderModel.cs
public class OrderModel : BaseModel
{
private DateTime _orderDate;
public DateTime OrderDate
{
get { return _orderDate; }
set { _orderDate = value; OnPropertyChanged(); }
}
private string _code;
public string Code
{
get { return _code; }
set { _code = value; OnPropertyChanged(); }
}
private int _qty;
public int Qty
{
get { return _qty; }
set { _qty = value; OnPropertyChanged(); }
}
ProductModel.cs
public class ProductModel : BaseModel
{
private string _code;
public string Code
{
get { return _code; }
set { _code = value; OnPropertyChanged(); }
}
private int _program;
public int Program
{
get { return _program; }
set { _program = value; OnPropertyChanged(); }
}
DataGrid 在 OrderView.xaml
<DataGrid ItemsSource="{Binding OrderListView}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Code}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Qty}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Program}"/> <- This from ProductList ??
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
OrderViewModel.cs
public class OrderViewModel : Screen
{
private readonly IDataConnection _connect;
private ICollectionView _orderView;
public ICollectionView OrderListView
{
get => _orderView;
set
{
_orderView = value;
NotifyOfPropertyChange(() => OrderListView);
}
}
private BindableCollection<OrderModel> _orderList;
public BindableCollection<OrderModel> OrderList
{
get => _orderList;
set
{
_orderList = value;
NotifyOfPropertyChange(() => OrderList);
}
}
private BindableCollection<ProductModel> _productList;
public BindableCollection<ProductModel> ProductList
{
get { return _productList; }
set { _productList = value; }
}
private string _code;
public string Code
{
get { return _code; }
set { _code = value; NotifyOfPropertyChange(() => Code); }
}
private int _qty;
public int Qty
{
get { return _qty; }
set { _qty = value; NotifyOfPropertyChange(() => Qty); }
}
private int _program;
public int Program
{
get { return _program; }
set
{
_program = value;
NotifyOfPropertyChange(() => Program);
}
}
public OrderViewModel(IDataConnection connect)
{
DisplayName = "Orders";
var allOrders = await _connect.Orders_GetByDateRange(StartDate, EndDate);
OrderList = new BindableCollection<OrderModel>(allOrders);
OrderListView = CollectionViewSource.GetDefaultView(OrderList);
var allProducts = await _connect.Products_GetAll();
ProductList = new BindableCollection<ProductModel>(allProducts);
}
}
基本上我想将关联程序拉入列中的模型中的“代码”匹配位置。
【问题讨论】: