1

I'm learning wpf with mvvm light and i'm face an issue. I've a usercontrol with a datagrid that's loaded with a query.

Now i'd like to add an event on the selectedIndex to retrieve the selected line and after that i'll pass the value to an other user control.

the issue is that my event is never fired.

Here is what i did

<DataGrid HorizontalAlignment="Left" Name="dgUser" VerticalAlignment="Top" SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" SelectedItem="{Binding selectedRow, Mode=TwoWay}"  Width="800" Height="253" >

Here is the code of my viewmodel

public RelayCommand selectedRow { get; private set; }
    private int _selectedIndex;
    public int SelectedIndex
    {
        get { return _selectedIndex; }
        set
        {
            _selectedIndex = value;
            RaisePropertyChanged("SelectedIndex");
        }
    }
    /// <summary>
    /// Initializes a new instance of the ListUserViewModel class.
    /// </summary>
    public ListUserViewModel()
    {
        selectedRow = new RelayCommand(() => SelectedRowChange());
        SelectedIndex = 2;
    }

    private void SelectedRowChange()
    {
        System.Windows.MessageBox.Show("test");
    }

My row is not selected with SelectedXIndex = 2 and nothing happen when i select an other row

user1898765
  • 323
  • 1
  • 6
  • 18

1 Answers1

1

You are trying to bind a Command on your ViewModel to the SelectedItem of your DataGrid and that's not quite how it works.

You want to hook into the DataGrid.SelectionChanged event and presumably fire your ViewModel's RelayCommand at that point.

Check this answer for a great explanation on how to do this in XAML.

EDIT:

To pass the SelectedItem to your RelayCommand as a command parameter, use the following in your XAML:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <i:InvokeCommandAction Command="{Binding MyCommand}" 
        CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

and ensure that you change your RelayCommand to be of the type you wish.

NOTE: You have not provided what the type of the items you have in your DataGrid, so I'll use object here.

public RelayCommand<object> selectedRow { get; private set; }

You can then rely on the command's parameter being the selected item:

public ListUserViewModel()
{
    selectedRow = new RelayCommand(i => SelectedRowChange(i));
    SelectedIndex = 2;
}

private void SelectedRowChange(object selectedItem)
{
    // selectedItem is the item that has just been selected
    // do what you wish with it here
    System.Windows.MessageBox.Show("test");
}
Community
  • 1
  • 1
Daniel May
  • 8,156
  • 1
  • 33
  • 43
  • thanks i use EventTrigger and my event is fired with messagebox but how can i have the value of the selected row? – user1898765 Jul 31 '14 at 11:22
  • You can pass the `SelectedItem` as a parameter to your `RelayCommand` through the other examples in that post. If this answered your question, please click the green tick on the left. – Daniel May Jul 31 '14 at 11:27
  • Could you explain me how is use commandparameter? and my row is not from a model but with a query.tolist() – user1898765 Jul 31 '14 at 11:35
  • Updated. If you have any further questions please ask another question. – Daniel May Jul 31 '14 at 11:40
  • The command parameter. `RelayCommand`. You need to read more about WPF and MVVM Light if you do not understand this. – Daniel May Jul 31 '14 at 11:48