2013-04-25 50 views
0

我想通知从数据网格中选择了一个项目,因为我从数据网格中选择一个项目时打开了一个模式窗口。我正在编辑模态窗口中的选定项目,并且由于我不想为选定项目设置RaisedPropertychanged机制,因为当我尝试修改选定项目时,它会打开另一个模式窗口。我现在试图使用事件触发器来解决这个问题,但得到错误。下面是相关的代码:如何通知从wpf数据网格中选择的项目并使用mvvm?

视图模型:

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> 

如果删除了触发部然后数据网格填充有所需的数据。如果包括触发代码,然后我得到这个错误信息:

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

我想知道有没有其他的方法来解决这样的事情。我正在使用MVVM Light工具包。

回答

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> 

UPDATE

你可能也应该使用

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

和修改VM您的命令。

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

谢谢,它解决了一个问题。现在数据网格正在填充数据。但是,当我选择数据网格中的项目时,触发器不起作用。你能帮我解释我的触发机制有什么问题吗? – User1551892 2013-04-25 14:56:39

+2

我很高兴它有帮助。我不知道你的新的问题,因为我不是用MVVM,光照充足熟悉,但是当我尝试这不发送PARAM它的工作原理,但我认为你应该使用'PassEventArgsToCommand = TRUE',而不是'CommandParameter = ..'然后相应地在VM中修改您的命令。 – 2013-04-25 15:08:09

+0

@ User1551892看看我更新的答案。 – 2013-04-25 15:12:51

相关问题