Madwolf
Madwolf

Reputation: 13

Command Parameter for Datagrid row double clicked

I have a user control with DataGrid. Grid is bounded to list of observable collection of StoreSales. Where StoreSales is a class with properties like Store Name, Number, etc. All these properties are columns on the datagrid.

Want to achieve: On double click of any row trigger a Relay Command on the ViewModel(Click_command) were I retrieve the storenumber for that row. I am able to trigger the RelayCommand Click_command.

I should do something like RelayCommand<string> and pass the storenumber from the CommandParamter, but dont know how.

Thanks in advance.

This is what I have

Xaml:

<UserControl x:Class="MyProject.StoreList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
             xmlns:vm="clr-namespace:Charlie.UI.ViewModel"
             xmlns:ch="clr-namespace:Charlie.UI"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
 <Grid>
     <DataGrid  IsReadOnly="True" ItemsSource="{Binding Path=StoreList}" AutoGenerateColumns="False" Name="StoreList" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="#"  Binding="{Binding Path=StoreNumber}" />
                <DataGridTextColumn Header="StoreName"  Binding="{Binding Path=StoreName}"/>
                <DataGridTextColumn Header="Total" Binding="{Binding Path=Total, StringFormat=C}"/>


        </DataGrid.Columns>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Path=Click_command, Mode=OneWay}" PassEventArgsToCommand="True" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </DataGrid>
        </Grid>

ViewModel:

 public RelayCommand Click_command
{
      get;
      private set;
}

private ObservableCollection<StoreSales> _StoreList;
    public ObservableCollection<StoreSales> StoreList 
    {
        get { return _StoreList; }
        set
        {
            _StoreList = value;
            RaisePropertyChanged("StoreList");
        }
    }

//Constructor

public StoreList()
{
      this.Click_command = new RelayCommand(() => Execute_me());
}

public void Execute_me()
{
   //Do something with store number           
}

Upvotes: 1

Views: 1465

Answers (1)

Anatolii Gabuza
Anatolii Gabuza

Reputation: 6260

To get this working you have several options:

  1. Define SelectedStore property in your ViewModel. Bind it to SelectedItem from DataGrid. Do not pass anything as a parameter to command and use created property to get StoreNumber;
  2. Specify binding: {Binding SelectedItem.StoreNumber, ElementName=MyDataGrid} to pass number as a CommandParamter;
  3. Use PassEventArgsToCommand="True" and update command definition little bit. Then by accessing sender you'll get SelectedItem (I don't like this approach, but still it exists);

For third option (which is less preferable from MVVM perspective) command should look following:

public RelayCommand Click_command
{
      get
      {
          if (this.click_command == null)
          {
                this.click_command = new RelayCommand<MouseButtonEventArgs>((args) => this.Execute_me(args));
          }
          return this.click_command;
      }
}

Upvotes: 2

Related Questions