0

I am trying to execute an action SelectedItem on row selected with DataGrid. I have a lot of code so I just put here the relevant parts. If you need more information please tell me.

When I click the row it's selected and the action is executed. It opens another window and lets the user do something there. I also want to let one chose this row again and Action this Set method again, but it's doing nothing until I chose another row and only after I chose another row it lets me get the first row.

In short, I want to be able to execute an action on selecting row1 and then execute it again on selecting row1 again.

My XAML code:

<DataGrid IsReadOnly="True" Name="DataGridProject" ItemsSource="{Binding ProjectList}"
AutoGenerateColumns="False" FontSize="22"
SelectedItem="{Binding SelectedRowItem,Mode=TwoWay}" DockPanel.Dock="Bottom" MaxHeight="300"
MinHeight="350" Height="350" SelectionMode="Single"/>

My ModelView Code:

public class PreTestInformationProjectAccessVM : INotifyPropertyChanged
{
    public DataRowView SelectedRowItem
    {
        get
        {
            return _SelectedRowItem;
        }
        set
        {
            _SelectedRowItem = value;
             // ...        
        }
    }
}
CSDev
  • 3,177
  • 6
  • 19
  • 37
  • 1
    So, an item is selected and you want do perform some action if the user clicks again on the already selected item? In that case `INotifypropertyChanged` won't help much, since the `SelectedRowItem` didn't change. I guess you will have to use Mouse events (`MouseLeftButtonDown`, `Click`,...) for that... – Roland Deschain Jul 29 '19 at 10:11
  • 1
    This should help you: https://stackoverflow.com/questions/51786072/selectionchanged-event-does-not-fire-after-reselecting-datagrid-row – Arif Eqbal Jul 29 '19 at 10:14
  • @RolandDeschain it is fine when trying to using MVVM style? –  Jul 29 '19 at 10:28
  • 1
    Sure, there is no law against having ANY code in the code behind in MVVM, however if you are a purist, you can also look into blend interactivity features as explained in this post here: https://stackoverflow.com/a/7877807/7745011 This way, you can still handle your events nicely in the viewmodel. – Roland Deschain Jul 29 '19 at 10:33

1 Answers1

0

Select current item can't Trigger selectedItem changed event,you can try mouse events like:

<DataGrid.RowStyle>
     <Style TargetType="DataGridRow">
           <EventSetter Event="MouseDown"  Handler="Click_Do" />
     </Style>
</DataGrid.RowStyle>
melody zhou
  • 148
  • 4