在使用MVVM时,我正在努力解决如何连接互连组合框的问题。我有一个代表订单的DTO,其中包含CustomerId
和OrderTypeId
。然后将这些包裹在OrderViewModel
内。我还有一个EditOrderViewModel
,它从db加载一个Customers列表。
我想要做的是从DB加载订单(类似于加载功能)在ComboBox中选择正确的项目(项目源是List,在文本中显示所选客户的名称)阻止组合框右侧,最后在下一个组合框中加载属于该Customer的OrderType列表,再次选择正确的OrderType,并在右侧的TextBlock中显示OrderTypeName。
当我从组合框中使用SelectedItem时,我设法让一些行为起作用,但这只是当我手动选择项时,因为我不确定在我的viewmodel中我如何转换Order.CustomerId(类型int)进入正确的SelectedItem(类型为CustomerDTO)。下面是一些代码,它们总体上显示了我在使用内存数据源时要实现的目标。谢谢Alex
<ComboBox Height="25" Width="150" ItemsSource="{Binding Path=Customers}" SelectedValue="{Binding Path=Order.CustomerId}" SelectedValuePath="Id">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Code}"></TextBlock>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Text="{Binding Path=Name,Mode=OneWay,NotifyOnSourceUpdated=True}"></TextBlock>
public class EditOrderViewModel : VMBase
{
public OrderViewModel Order{get;set;}
public void Load()
{
Order = new OrderViewModel(new OrderDto{CustomerId=1,OrderTypeId=2});
Order.PropertyChanged += MainWindowViewModel_PropertyChanged;
}
public EditOrderViewModel()
{
Order = new OrderViewModel(new OrderDto());
Order.PropertyChanged += OrderViewModel_PropertyChanged;
Customers = new List<CustomerDto> {
new CustomerDto{ Id = 1, Code = "ACME", Name = "ACME CORP" },
new CustomerDto{ Id = 2, Code = "MSFT", Name="MICROSOFT CORP" },
new CustomerDto{ Id = 3, Code = "APP", Name = "APPLE" }};
OrderTypes = new List<OrderTypeDto>{
new OrderTypeDto{OrderTypeId=1, CustomerId =1, Name = "Cake Order"},
new OrderTypeDto{OrderTypeId=2, CustomerId =1, Name = "Sandwich Order"},
new OrderTypeDto{OrderTypeId=3, CustomerId =2, Name = "Chocolate Order"},
new OrderTypeDto{OrderTypeId=4, CustomerId =2, Name = "Bread Order"},
new OrderTypeDto{OrderTypeId=5, CustomerId =3, Name = "Drinks Order"}};
}
void OrderViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "OrderTypeId":
break;
case "SelectedCustomer":
break;
default:
break;
}
}
public List<OrderTypeDto> OrderTypes { get; set; }
public List<CustomerDto> Customers { get; set; }
}
public class OrderDto
{
public int CustomerId { get; set; }
public int OrderTypeId { get; set; }
}
public class CustomerDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
}
public class OrderViewModel : VMBase
{
private OrderDto _orderDto;
private string _customerName;
public OrderViewModel(OrderDto orderDto)
{
_orderDto = orderDto;
}
public int CustomerId {
get { return _orderDto.CustomerId; }
set
{
_orderDto.CustomerId = value;
RaisePropertyChanged("CustomerId");
}
}
public string CustomerName {
get { return _customerName; }
set {_customerName = value;
RaisePropertyChanged("CustomerName");
}
}
public int OrderTypeId
{
get { return _orderDto.OrderTypeId; }
set
{
_orderDto.OrderTypeId = value;
RaisePropertyChanged("OrderTypeId");
}
}
}
答案 0 :(得分:0)
不要在XAML绑定中设置ComboBox.SelectedValue
。您应该将ComboBox.SelectedItem
绑定到模型,以便轻松获得CustomDTO。您应该向OrderViewModel添加一个名为Customer
(类型为CustomerDTO )的属性,而不是尝试使用多个属性( CustomerID,CustomerName等)重新创建CustomerDTO。 )。