0

I want to binding handler in EventSetter so my code till now

<DataGrid  IsReadOnly="True" x:Name="OverviewDatagrid" ItemsSource="{Binding MyCollection}"
                  AutoGenerateColumns="False"
                  CanUserAddRows="False">
            <DataGrid.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                   Color="#404040"/>
            </DataGrid.Resources>
            <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <EventSetter Event="MouseDown" Handler="DataGridCell_MouseDoubleClick"/>
                    </Style>
                </DataGrid.CellStyle>
            <DataGrid.Columns>

                <DataGridTextColumn Width="300" Header="FirstName" Binding="{Binding Code, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <DataGridTextColumn Header="LastName" Binding="{Binding LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

            </DataGrid.Columns>
        </DataGrid>

When i try to binding Handler it return me exception

 <EventSetter Event="MouseDown" Handler="{Binding ClickCommand}"/>

So ViewModel.cs

 private ICommand _clickCommand;
        public ICommand ClickCommand
        {
            get
            {
                return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(), () => CanExecute));
            }
        }
        public bool CanExecute
        {
            get
            {
                // check if executing is allowed, i.e., validate, check if a process is running, etc. 
                return true;
            }
        }

        public void MyAction()
        {
        }

And CommandHandler.cs file

public class CommandHandler : ICommand
    {
        private Action _action;
        private Func<bool> _canExecute;

        /// <summary>
        /// Creates instance of the command handler
        /// </summary>
        /// <param name="action">Action to be executed by the command</param>
        /// <param name="canExecute">A bolean property to containing current permissions to execute the command</param>
        public CommandHandler(Action action, Func<bool> canExecute)
        {
            _action = action;
            _canExecute = canExecute;
        }

        /// <summary>
        /// Wires CanExecuteChanged event 
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// Forcess checking if execute is allowed
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public bool CanExecute(object parameter)
        {
            return _canExecute.Invoke();
        }

        public void Execute(object parameter)
        {
            _action();
        }
    }

I am new to c# and i dont know why it return me This line return error.

>  <EventSetter Event="MouseDown" Handler="{Binding ClickCommand}"/>

System.NullReferenceException: 'Object reference not set to an instance of an object.'

  • Something is `null`, since you didn't show [mcve], nor complete error, nor tell us at which line you get it, we can't help you. – Sinatr Jul 20 '20 at 10:24
  • no of course did you read question? – Zarko Petrov Jul 20 '20 at 10:24
  • *"I am new to c# and i dont know why it return me"* - then read duplicate carefully, the accepted answer contains a lot of useful information. – Sinatr Jul 20 '20 at 10:26
  • @Sinatr see edit – Zarko Petrov Jul 20 '20 at 10:26
  • See if [this topic](https://stackoverflow.com/q/42023994/1997232) is helpful, seems to be the same mistake you are doing. – Sinatr Jul 20 '20 at 10:28
  • Note that events and command are different things. An EventSetter can't be used to bind to an ICommand property. – Clemens Jul 20 '20 at 11:13
  • An event handler isn't a command. Square peg. Round hole. And your execute is expecting a parameter that you're not giving it. Do you need double click or would click work? You could template your cells so they're actually buttons templated as textblock. You can then bind a command to the button's command property. Click the button and the command would be invoked. – Andy Jul 20 '20 at 11:14

0 Answers0