如何通知从wpf中的数据网格中选择并使用mvvm?

时间:2013-04-25 14:38:56

标签: wpf mvvm mvvm-light

我正在尝试通知从数据网格中选择了一个项目,因为我在从数据网格中选择项目时打开一个模态窗口。我正在模态窗口中编辑所选项目,因此我不想为所选项目使用RaisedPropertychanged机制,因为当我尝试修改所选项目时,它会打开另一个模态窗口。我现在正在尝试使用事件触发器来解决此问题,但会收到错误。以下是相关代码:

视图模型:

  public ObservableCollection<Student> sItems {
  get {
    return ssitems;
  }
 set {
   ssitems = value;
   RaisePropertyChanged( "sItems" );
  }
} 
private StudentsInformation studentInformation;
   public StudentsInformation studentInformationObject {
     get {
       return studentInformation;
     }
     set {
       studentInformation = value;
       RaisePropertyChanged( "studentInformationObject" );
     }
   }



public RelayCommand<Student> SelectionChangedCommand {
      get;
      set;
    }

这些代码行在构造函数中:

SelectionChangedCommand = new RelayCommand<Student>(
          item => {
            if( item != null ) {
              MessengerInstance.Send<Student>( item, "SelectedStudent" );
            }
          } );

这是与datagarid绑定的集合。

查看:

 <DataGrid x:Name="dataGrid" Grid.Row="1" Margin="5"
                              IsReadOnly="False"  ColumnHeaderHeight="30"
                              ItemsSource="{Binding Path=sItems}" AutoGenerateColumns="False" 
                              SelectedItem="{Binding Path=SelectedStudentObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                        <DataGrid.Columns>
                            <!--<DataGridCheckBoxColumn Header="Select" Binding="{Binding Path=myselect, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" />-->
                            <DataGridTextColumn Header="Name" Binding="{Binding name}"></DataGridTextColumn>
                            <DataGridTextColumn Header="Enrollment" Binding="{Binding enrollment}"></DataGridTextColumn>
                            <DataGridTextColumn Header="Score" Binding="{Binding score}"></DataGridTextColumn>
                            <DataGridTextColumn Header="Comment" Binding="{Binding comment}"></DataGridTextColumn>
                        </DataGrid.Columns>
                        <i:EventTrigger EventName="SelectionChanged">
                            <cmd:EventToCommand Command="{Binding SelectionChangedCommand}"
                                                CommandParameter="{Binding SelectedItem}" />
                        </i:EventTrigger>
                    </DataGrid>

如果我删除了触发器部分,那么datagrid会填充所需的数据。如果包括触发器代码,那么我收到此错误消息:

在使用ItemsSource之前,项目集合必须为空。

我想知道是否有其他方法来解决这类问题。我正在使用MVVM Light工具包。

1 个答案:

答案 0 :(得分:2)

该事件触发器应该在其他地方。它应放在Interaction.Triggers

像这样使用:

<DataGrid...>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <cmd:EventToCommand Command="{Binding SelectionChangedCommand}"
                               CommandParameter="{Binding SelectedItem}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</DataGrid> 

<强>更新

你可能也应该使用

<cmd:EventToCommand x:Name="SelectionChanged" 
                        Command="{Binding SelectionChangedCommand}" 
                        PassEventArgsToCommand="True" />

并在VM中修改您的命令。

RelayCommand<SelectionChangedEventArgs> SelectionChangedCommand{get; private set;}