MVVMWPFmaster-detailcombobox感谢之前在StackOverflow上的一些建议,让我对MVVM的理解有了很大的进步。然而,当事情开始变得更加复杂时,我仍在苦苦挣扎。我下面的观点是为了输入订单。它绑定到OrderScreenViewModel的DataContext。第一个组合框用于选择客户。第二个组合框用于为新的OrderLine选择ProductCode。有些项目我不知道如何在MVVM中实现:1)在选择客户时更新Productscombobox,以便其项目源仅显示与在组合框中选择的CustomerDto记录具有相同CustomerId的产品2)调用Load时,在Customers组合框中设置SelectedItem,使其显示CustomerId等于OrderDto上的CustomerId。3)应用与1)相同的过程,以便仅显示/加载属于该客户的产品,并将Productscombobox上的SelectedItem设置为指向与OrderLineDto中包含的ProductId相同的条目我不知道如何进行,或者即使我的视图模型的责任是正确的。也许它与NotifyPropertyChanged有关?任何关于如何实现上述目标的指示将不胜感激。如果我做对了,我相信它会对我的申请有很大帮助。非常感谢亚历克斯。公共类OrderScreenViewModel{publicWMSOrderViewModelOrder{get;私有集;}publicWMSOrderLineViewModelCurrentLine{get;私有集;}publicOrderScreenViewModel(){Order=newWMSOrderViewModel();CurrentLine=newWMSOrderLineViewModel(newOrderLineDto());}publicvoidLoad(intorderId){varorderDto=newOrderDto{CustomerId=1,Lines=newList{newOrderLineDto{ProductId=1}}};Order=newWMSOrderViewModel(orderDto);}publicListCustomers{get{returnnewList{newCustomerDto{CustomerId=1,CustomerCode="Apple"},newCustomerDto{CustomerId=1,CustomerCode="Microsoft"},};}}publicListProducts{get{returnnewList{newProductDto{CustomerId=1,ProductId=1,ProductCode="P100",Description="Pepsi"},newProductDto{CustomerId=1,ProductId=2,ProductCode="P110",Description="Coke"},newProductDto{CustomerId=2,ProductId=3,ProductCode="P120",Description="Fanta"},newProductDto{CustomerId=2,ProductId=4,ProductCode="P130",Description="雪碧"}};}}公共类WMSOrderLineViewModel{privateProductDto_product;私人OrderLineDto_orderLineDto;publicWMSOrderLineViewModel(OrderLineDtoorderLineDto){_orderLineDto=orderLineDto;}publicProductDtoProduct{get{return_product;}设置{_产品=价值;RaisePropertyChanged("产品");}}公共类WMSOrderViewModel{私人ObservableCollection_lines;私人OrderDto_orderDto;publicObservableCollectionLines{get{return_lines;}}privateCustomerDto_customer;publicCustomerDtoCustomer{get{return_customer;}set{_customer=value;RaisePropertyChanged("Customer")}publicWMSOrderViewModel(OrderDtoorderDto){_orderDto=orderDto;_lines=newObservableCollection();foreach(varlineDtoinorderDto.Lines){_lines.Add(newWMSOrderLineViewModel(lineDto));}}publicWMSOrderViewModel(){_lines=newObservableCollection();您需要使用产品和客户按键输入ObservableCollection当你在viewmodel中改变这些observablecollections时,它们会更新视图,因为OC已经实现了INotifyPropertyChanged。Order和CurrentLine应该只是一种类型,而不是真正的ViewModel。1)当在Customercombobox的SelectedItem上调用setter时,您必须执行此操作。2)您可能需要在OrderScreenViewModel的ctr中执行此操作,方法是使用您的逻辑来确定客户也更改了CurrentLine.Customer。如果您在ctr中执行此操作,则该值将在绑定发生之前设置。3)此外,每当您对组合框绑定到的ObservableCollection进行更改时,它都会更新UI。如果更改SelectedItem的绑定对象,请确保调用RaisedPropertyChanged事件。ETA:将xaml更改为此,SelectedProduct和SelectedCustomer绑定到SelectedItem属性,这应该让您朝着正确的方向开始,一切都是由客户ID构建的,客户和产品的所有逻辑都需要在您的存储库中进行。公共类OrderScreenViewModel:INotifyPropertyChanged{privatereadonlyIProductRepository_productRepository;私有只读ICustomerRepository_customerRepository;publicOrderScreenViewModel(IProductRepositoryproductRepository,ICustomerRepositorycustomerRepository){_productRepository=productRepository;_customerRepository=customerRepository;建立客户集合();}privatevoidBuildCustomersCollection(){varcustomers=_customerRepository.GetAll();foreach(varcustomerincustomers)_customers.Add(customer);私人ObservableCollection_customers=newObservableCollection();publicObservableCollectionCustomers{get{return_customers;}私有集{_customers=value;}}privateObservableCollection_products=newObservableCollection();publicObservableCollectionProducts{get{return_products;}私有集{_products=value;}}私人客户_selectedCustomer;公共客户SelectedCustomer{get{return_selectedCustomer;}设置{_selectedCustomer=值;PropertyChanged(这个,新的PropertyChangedEventArgs(“SelectedCustomer”));BuildProductsCollectionByCustomer();}}私有产品_selectedProduct;公共产品SelectedProduct{get{return_selectedProduct;}设置{_selectedProduct=值;PropertyChanged(这个,新的PropertyChangedEventArgs(“SelectedProduct”));DoSomethingWhenSelectedPropertyIsSet();}}privatevoidDoSomethingWhenSelectedPropertyIsSet(){//省略}privatevoidBuildProductsCollectionByCustomer(){varproductsForCustomer=_productRepository.GetById(_selectedCustomer.Id);foreach(产品中的var产品){_products.Add(产品);}}公共事件PropertyChangedEventHandlerPropertyChanged=delegate{};}publicinterfaceICustomerRepository:IRepository{}publicclassCustomer{publicintId{get;放;}}publicinterfaceIProductRepository:IRepository{}publicclassProduct{}这是标看起来像一个IRepository,叫做repository模式:以上就是C#学习教程:MVVMWPFmasterdetailcombobox的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——publicinterfaceIRepository{IEnumerableGetAll();TGetById(intid);无效保存(TsaveThis);voidDelete(TdeleteThis);转载请注明出处:
