将cellediteventargs传递给MVVM中的命令

时间:2019-02-06 10:00:39

标签: mvvm command parameter-passing

我似乎无法通过命令将“ DataGridCellEditEventArgs e”传递给MVVM,在没有MVVM的项目中,我使用它来检索更改的值。因此,我想通过args,但是无论我做什么,它都说e为空。我已经坚持了好几天,我尝试了很多事情。我只是无法传递更改后的值。

ie代表:             xmlns:ie =“ clr-namespace:System.Windows.Interactivity; assembly = System.Windows.Interactivity”

命令类的代码

namespace SgvConfiguratorMVVM.Tools
{
    public class EditCommand<T> : ICommand
    {
        public event EventHandler CanExecuteChanged;
        private Action<T> _action;

        public EditCommand(Action<T> action)
        {
            _action = action;
        }
        public bool CanExecute(object parameter) { return true; }

        public void Execute(object parameter)
        {
            if(_action != null)
            {
                var castParameter = (T)Convert.ChangeType(parameter,
                         typeof(T));
                _action(castParameter);
            }
        }
    }
}

来自xaml的代码

   <DataGrid Name="OrderGrid"
              Width="250"
              IsReadOnly="False"
              AutoGenerateColumns="False"                          
              SelectionMode="Single"                          
              RowHeight="45"                          
              FontSize="10"    
              Margin="10 10 0 0"
              ItemsSource="{Binding OrderList}"  
              SelectedItem="{Binding SelectedOrder}"
              CanUserAddRows="False"                          
              CanUserDeleteRows="False">
    <ie:Interaction.Triggers>
        <ie:EventTrigger EventName="CellEditEnding">
            <ie:InvokeCommandAction Command="{Binding OG_CellEditEndingCommand}"/>                                          
        </ie:EventTrigger>
    </ie:Interaction.Triggers>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding MaxOrders}" Header="MaxOrders"/>
        <DataGridTextColumn Binding="{Binding MaxAssigned}" Header="MaxAssigned"/>
        <DataGridTextColumn Binding="{Binding AvailableForOrders}" Header="AvailableForOrders"/>
    </DataGrid.Columns>
</DataGrid>

viewmodel中的代码

 private void UpdateCells(object e)
        {
            var test = e;
            // always null
        }

        public ShowOperationsViewModel(string locationname)
        {            
            this.OG_CellEditEndingCommand = new EditCommand<object>(e => UpdateCells(e));
        }

0 个答案:

没有答案