感谢我之前在Stack Overflow上提出的一些建议,我在理解MVVM方面取得了很大进展。然而,当事情开始变得更加复杂时,我仍然在苦苦挣扎。
我有以下视图,用于输入订单。它绑定到OrderScreenViewModel的DataContext。
<StackPanel>
<ComboBox Height="25" Width="100" DisplayMemberPath="CustomerCode" SelectedItem="{Binding Path=Order.Customer}" ItemsSource="{Binding Path=Customers}"></ComboBox>
<ComboBox Height="25" Width="100" DisplayMemberPath="ProductCode" SelectedItem="{Binding Path=CurrentLine.Product}" ItemsSource="{Binding Path=Products}"></ComboBox>
</StackPanel>
第一个组合框用于选择客户。第二个组合框用于为新的OrderLine选择ProductCode。
有些项目我无法解决如何在MVVM中实现:
1)当选择客户时,更新Products组合框,以便其项目源仅显示与组合框中选择的CustomerDto记录具有相同CustomerId的产品
2)当调用Load时,在Customers组合框中设置SelectedItem,以便显示CustomerId等于OrderDto上的CustomerId。
3)应用,与1)相同的过程,以便只显示/加载属于该客户的产品,并在Products组合框上设置SelectedItem,使其指向具有相同ProductId的条目包含在OrderLineDto
我不知道如何继续,或者即使我的视图模型的责任是正确的。也许它与NotifyPropertyChanged有关?任何有关如何实现上述目标的指示将不胜感激。我相信如果我做对了,它会在我的应用程序中帮助我。非常感谢Alex。
public class OrderScreenViewModel
{
public WMSOrderViewModel Order { get; private set; }
public WMSOrderLineViewModel CurrentLine { get; private set; }
public OrderScreenViewModel()
{
Order = new WMSOrderViewModel();
CurrentLine = new WMSOrderLineViewModel(new OrderLineDto());
}
public void Load(int orderId)
{
var orderDto = new OrderDto { CustomerId = 1, Lines = new List<OrderLineDto> { new OrderLineDto{ProductId = 1 }} };
Order = new WMSOrderViewModel(orderDto);
}
public List<CustomerDto> Customers
{
get{
return new List<CustomerDto> {
new CustomerDto{CustomerId=1,CustomerCode="Apple"},
new CustomerDto{CustomerId=1,CustomerCode="Microsoft"},
};
}
}
public List<ProductDto> Products
{
get
{
return new List<ProductDto> {
new ProductDto{CustomerId=1,ProductId=1,ProductCode="P100",Description="Pepsi"},
new ProductDto{CustomerId=1,ProductId=2,ProductCode="P110",Description="Coke"},
new ProductDto{CustomerId=2,ProductId=3,ProductCode="P120",Description="Fanta"},
new ProductDto{CustomerId=2,ProductId=4,ProductCode="P130",Description="Sprite"}
};
}
}
public class WMSOrderLineViewModel
{
private ProductDto _product;
private OrderLineDto _orderLineDto;
public WMSOrderLineViewModel(OrderLineDto orderLineDto)
{
_orderLineDto = orderLineDto;
}
public ProductDto Product { get { return _product; }
set{_product = value; RaisePropertyChanged("Product"); }
}
public class WMSOrderViewModel
{
private ObservableCollection<WMSOrderLineViewModel> _lines;
private OrderDto _orderDto;
public ObservableCollection<WMSOrderLineViewModel> Lines { get { return _lines; } }
private CustomerDto _customer;
public CustomerDto Customer { get{return _customer;} set{_customer =value; RaisePropertyChanged("Customer") }
public WMSOrderViewModel(OrderDto orderDto)
{
_orderDto = orderDto;
_lines = new ObservableCollection<WMSOrderLineViewModel>();
foreach(var lineDto in orderDto.Lines)
{
_lines.Add(new WMSOrderLineViewModel(lineDto));
}
}
public WMSOrderViewModel()
{
_lines = new ObservableCollection<WMSOrderLineViewModel>();
}
}
答案 0 :(得分:4)
您需要使产品和客户键入ObservableCollection。
当您在viewmodel中更改这些observablecollections时,它们将更新视图,因为OC已经实现了INotifyPropertyChanged。
Order和CurrentLine应该只是一个类型而不是真正被称为ViewModel。
1)当选择Customer组合框的SelectedItem上调用setter时,你将不得不这样做。
2)您可能需要在OrderScreenViewModel的ctr中执行此操作,方法是使用您的逻辑来确定Customer更改CurrentLine.Customer的内容。如果你在ctr中这样做,这将在绑定发生之前设置值。
3)同样,只要您对组合框绑定的ObservableCollection进行更改,它就会更新UI。如果您对SelectedItem绑定的内容进行了更改,请确保调用RaisedPropertyChanged事件。
ETA:将xaml更改为this,绑定到SelectedItem属性的SelectedProduct和SelectedCustomer
<StackPanel>
<ComboBox Height="25" Width="100" DisplayMemberPath="CustomerCode" SelectedItem="{Binding Path=SelectedCustomer}" ItemsSource="{Binding Path=Customers}"></ComboBox>
<ComboBox Height="25" Width="100" DisplayMemberPath="ProductCode" SelectedItem="{Binding Path=SelectedProduct}" ItemsSource="{Binding Path=Products}"></ComboBox>
</StackPanel>
这应该让你开始朝着正确的方向前进,所有的东西,所有用户建立客户和产品的逻辑都需要在你的知识库中发生。
public class OrderScreenViewModel : INotifyPropertyChanged
{
private readonly IProductRepository _productRepository;
private readonly ICustomerRepository _customerRepository;
public OrderScreenViewModel(IProductRepository productRepository,
ICustomerRepository customerRepository)
{
_productRepository = productRepository;
_customerRepository = customerRepository;
BuildCustomersCollection();
}
private void BuildCustomersCollection()
{
var customers = _customerRepository.GetAll();
foreach (var customer in customers)
_customers.Add(customer);
}
private ObservableCollection<Customer> _customers = new ObservableCollection<Customer>();
public ObservableCollection<Customer> Customers
{
get { return _customers; }
private set { _customers = value; }
}
private ObservableCollection<Product> _products = new ObservableCollection<Product>();
public ObservableCollection<Product> Products
{
get { return _products; }
private set { _products = value; }
}
private Customer _selectedCustomer;
public Customer SelectedCustomer
{
get { return _selectedCustomer; }
set
{
_selectedCustomer = value;
PropertyChanged(this, new PropertyChangedEventArgs("SelectedCustomer"));
BuildProductsCollectionByCustomer();
}
}
private Product _selectedProduct;
public Product SelectedProduct
{
get { return _selectedProduct; }
set
{
_selectedProduct = value;
PropertyChanged(this, new PropertyChangedEventArgs("SelectedProduct"));
DoSomethingWhenSelectedPropertyIsSet();
}
}
private void DoSomethingWhenSelectedPropertyIsSet()
{
// elided
}
private void BuildProductsCollectionByCustomer()
{
var productsForCustomer = _productRepository.GetById(_selectedCustomer.Id);
foreach (var product in Products)
{
_products.Add(product);
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
public interface ICustomerRepository : IRepository<Customer>
{
}
public class Customer
{
public int Id { get; set; }
}
public interface IProductRepository : IRepository<Product>
{
}
public class Product
{
}
以下是标准IRepository的样子,这称为存储库模式:
public interface IRepository<T>
{
IEnumerable<T> GetAll();
T GetById(int id);
void Save(T saveThis);
void Delete(T deleteThis);
}