locked
DataGrid SelectedItemChanged doesn't get triggered RRS feed

  • Question

  • I'm trying to detect when selected items change in the datagrid so that I can trigger some command. 

    When I do this:


             <toolkit:DataGridDragDropTarget 
                AllowDrop="True"
                Grid.Column="0"
                VerticalAlignment="Top"
                >
                <sdk:DataGrid 
                   Name="dataGrid1" 
                   ItemsSource="{Binding UnrelatedCategoriesView}"
                   AutoGenerateColumns="False" 
                   VerticalAlignment="Top" 
                   VerticalScrollBarVisibility="Visible"
                   SelectionMode="Extended"
                   >
    
                   <i:Interaction.Triggers>
                      <i:EventTrigger EventName="SelectionChanged">
                         <i:InvokeCommandAction
                            Command="{Binding SelectionUnrelatedChangedCommand}"
                            CommandParameter="{Binding SelectedItems, ElementName=dataGrid1}" 
                            />
                      </i:EventTrigger>
                   </i:Interaction.Triggers>
    
                   <sdk:DataGrid.Columns>
                      <sdk:DataGridTextColumn 
                         Header="Unrelated"
                         Binding="{Binding Name}"
                         IsReadOnly="True"
                         MinWidth="75"
                         />
                   </sdk:DataGrid.Columns>
                </sdk:DataGrid>
             </toolkit:DataGridDragDropTarget>
    


    It all works, EXCEPT if I click on the first row in the datagrid.  Then the Selection doesn't trigger and the command doesn't execute.

    If I add a SelectedItem={Binding myfield, Mode=TwoWay}, then the command is always triggered, BUT then, of course, it doesn't allow multiple selection.

    The final objective is just to have some buttons which are bound to commands to check their CanExecute when the selected items in the datagrid changes.

    So the command bound above just sets a selecteditems property in the view model and that then forces the CanExecute for the command bound to the button to be evaluated.  (if there are items selected, then the command needs to be enabled)

    Sunday, November 21, 2010 7:15 AM

Answers

  • That first item selection not triggering is only after the initial load of the page.  If I click on something else and THEN go back to the first row, it works.

    The first row might already been selected when the DataGrid load, so SelectionChanged event doesn't fire because there is no change in Selection. You can try to set SelectedIndex = -1 in your Loaded event to see if it works.


    Sunday, November 21, 2010 9:33 AM

All replies

  • That first item selection not triggering is only after the initial load of the page.  If I click on something else and THEN go back to the first row, it works.

    I was trying to make my Buttons activate conditionally, but if I unconditionally activate them and have the command on the button include a CommandParameter as the SelectedItems of the datagrid through element binding, it works fine.  That first row IS selected. 

    So I presume the problem is that when it starts, the first row is selected (even though it doesn't LOOK selected in the UI.  it has a sort of "semi-selected" look.  darker than normal, but not the selected darkness).  And if the first row is selected, then clicking on it doesn't trigger the "changed" event.

    Is there a way I could get the datagrid started that REALLY has nothing selected?  And in what event would I place that?  (I tried a couple of things before, but didn't come up with the right combination)

    Sunday, November 21, 2010 8:26 AM
  • That first item selection not triggering is only after the initial load of the page.  If I click on something else and THEN go back to the first row, it works.

    The first row might already been selected when the DataGrid load, so SelectionChanged event doesn't fire because there is no change in Selection. You can try to set SelectedIndex = -1 in your Loaded event to see if it works.


    Sunday, November 21, 2010 9:33 AM
  • I believe I already tried the -1 trick.  I've moved on to other problems and it might be a little while before I get back to this one and double check the -1 trick.

    Tuesday, December 7, 2010 10:13 AM