User1551892
User1551892

Reputation: 3384

How to notify an item has selected from data grid in wpf and using mvvm?

I am trying to notify that an item is selected from data grid because I am opening a modal window while selecting a item from data grid. I am editing selected item in modal window and due to which I do not want to RaisedPropertychanged mechanism for selected item because when I try to modify the selected item, it opens another modal window. I am now trying to use event trigger to fix this issue but getting errors. Below is related code:

ViewModel:

  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;
    }

These lines of code are in constructor:

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

This is collection which is bonded with datagarid.

View:

 <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>

If I remove the trigger section then datagrid populated with desired data. If Include the trigger code then I get this error message:

Items collection must be empty before using ItemsSource.

I would like to know is there other ways to fix this kind of things. I am using MVVM Light toolkit.

Upvotes: 0

Views: 1957

Answers (1)

Kapit&#225;n Ml&#237;ko
Kapit&#225;n Ml&#237;ko

Reputation: 4538

That event trigger should be elsewhere. It should be placed in Interaction.Triggers

Use it like this:

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

UPDATE

you probably should also use

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

and modify your Command in VM.

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

Upvotes: 3

Related Questions