2013-04-25 51 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

相關問題