【发布时间】:2012-02-29 10:02:54
【问题描述】:
感谢我之前在 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) 选择客户后,更新产品组合框,使其项目源仅显示具有与在组合框中选择的 CustomerDto 记录相同的 CustomerId
2) 调用 Load 时,在 Customers 组合框中设置 SelectedItem,以便它显示 CustomerId 等于 OrderDto 上的 CustomerId。
3) 应用,过程与 1) 相同,以便仅显示/加载属于该客户的产品,并在产品组合框中设置 SelectedItem,使其指向带有与 OrderLineDto 中包含的 ProductId 相同
我不确定如何继续,或者即使我的视图模型的职责是否正确。也许它与 NotifyPropertyChanged 有关?任何关于我如何实现上述目标的指示都将不胜感激。我相信如果我做对了,它将对我的应用程序有很大帮助。非常感谢亚历克斯。
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>();
}
}
【问题讨论】:
标签: c# wpf mvvm combobox prism