使用MVVMLight EventToCommand数据消失

时间:2014-11-20 05:20:20

标签: c# wpf mvvm mvvm-light

我已成功通过mvvmlight上的dotnetcurry教程:http://www.dotnetcurry.com/showarticle.aspx?ID=1037。这显示了如何使用MVVMLight通过EventToCommand发送消息,将数据从一个视图发送到另一个视图。我已经使用Northwind数据库对我的尝试进行编码,如果有帮助,可以压缩并发送解决方案。

我有一个包含许多视图的窗口。窗口的左侧窗格显示员工列表。选择员工会打开a)在中间窗格中显示员工详细信息,b)在右侧窗格中显示该员工处理的订单列表。 我正在尝试做的事情是为了点击列表中的其中一个订单,在底部窗格中显示订单详细信息(例如客户,订单行信息)。 会发生什么点击订单会使订单列表中的数据和员工详细信息视图消失。

After clicking a row in the Employee Table 单击Employee表中的行后的视图(请原谅UI,我知道它不漂亮)。

After clicking a row in the Order List 单击右侧“订单列表”中的一行后的视图。现在,视图下方的空白应填充有关所选订单的详细信息。

一些代码:
EmployeeInfo.xaml(员工列表)

<UserControl ...
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:mvvm="http://www.galasoft.ch/mvvmlight"
    DataContext="{Binding Main, Source={StaticResource Locator}}">
...
<DataGrid Grid.Row="2" ItemsSource="{Binding Employees}" x:Name="dgEmployees" IsReadOnly="True" >
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
      <mvvm:EventToCommand Command="{Binding SendEmployeeCommand, Mode=OneWay}" 
            CommandParameter="{Binding ElementName=dgEmployees, Path=SelectedItem}" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</DataGrid>

EmployeeOrderView.xaml(订单清单)

<Grid>
  <ListView Name="listOrders" ItemsSource="{Binding NewEmployee.Orders}" >
    <i:Interaction.Triggers>
      <i:EventTrigger EventName="SelectionChanged">
        <mvvm:EventToCommand Command="{Binding SendOrderCommand, Mode=OneWay}" 
              CommandParameter="{Binding ElementName=listOrders, Path=SelectedItem}"/>
      </i:EventTrigger>
    </i:Interaction.Triggers>
    <ListView.View>
      <GridView >
        <GridViewColumn DisplayMemberBinding="{Binding Path=OrderDate}" Header="Ordered" />
        <GridViewColumn DisplayMemberBinding="{Binding Path=Customer.CompanyName}" />
        <GridViewColumn DisplayMemberBinding="{Binding Path=ShippedDate}" Header="Shipped" />
      </GridView>
    </ListView.View>
  </ListView>
</Grid>

包含视图的窗口(MainWindow.xaml)只是一个由以下内容组成的网格:

<view:EmployeeInfo Grid.Column="0" Grid.Row="0" />
<view:SaveEmployeeView Grid.Column="1" Grid.Row="0" />
<view:EmployeeOrderView Grid.Column="2" Grid.Row="0" />
<view:OrderCustomerView Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" />

MainViewModel:

[PreferredConstructor]
public MainViewModel(IDataAccessService proxy)
{
  serviceProxy = proxy;
  Employees = new ObservableCollection<Employee>();
  ReadAllCommand = new RelayCommand(GetEmployees);
  NewEmployee = new Employee();
  SaveCommand = new RelayCommand<Employee>(SaveEmployee);
  SearchCommand = new RelayCommand(SearchEmployee);
  SendEmployeeCommand = new RelayCommand<Employee>(SendEmployee);
  ReceiveEmployee();
  SendOrderCommand = new RelayCommand<Order>(SendOrder);
  ReceiveOrderMessage();
}

public RelayCommand ReadAllCommand { get; set; }
public RelayCommand<Employee> SaveCommand { get; set; }
public RelayCommand SearchCommand { get; set; }
public RelayCommand<Employee> SendEmployeeCommand { get; set; }
public RelayCommand<Order> SendOrderCommand { get; set; }

// Properties (all properties in the same form as NewEmployee, but boilerplate code
// omitted for (some, at least) brevity)

public Employee NewEmployee
{
  get
  {
    return newEmployee;
  }
  set
  {
    if (newEmployee != value)
    {
      newEmployee = value;
      RaisePropertyChanged("NewEmployee");
    }
  }
}
private Employee newEmployee;

public ObservableCollection<Employee> Employees ...
private ObservableCollection<Employee> employees;

public string EmployeeName ...
private string employeeName;

public Order CurrentOrder...
private Order currentOrder;


void SendEmployee(Employee message)
{
  if (message != null)
  {
    Messenger.Default.Send<MessageCommunicator>(new MessageCommunicator()
    {
      EmployeeMessage = message
    });
  }
}

void ReceiveEmployee()
{
  if (NewEmployee != null)
  {
    Messenger.Default.Register<MessageCommunicator>(this, (emp) =>
      {
        this.NewEmployee = emp.EmployeeMessage;
      });
  }
}

void SendOrder(Order orderMessage)
{
  if (orderMessage != null)
  {
    Messenger.Default.Send<MessageCommunicator>(new MessageCommunicator()
      {
        OrderMessage = orderMessage
      });
  }
}

void ReceiveOrderMessage()
{
  if (CurrentOrder != null)
  {
    Messenger.Default.Register<MessageCommunicator>(this, (ord) =>
      {
        this.CurrentOrder = ord.OrderMessage;
      });
  }
}

我已经调试了,显示订单消息正确发送,但之后它跳转到汇编代码,我有点迷失(我认为它正在寻找mvvmlight源)。

对我而言,感觉就像gui事件导致这种情况发生 - 我原本以为是我没有正确使用mvvmlight的eventtocommand,但现在我不确定。任何人都可以解释为什么数据会消失或者它已经消失了吗?

0 个答案:

没有答案