1

I have a wpf c# app. I am trying to implement mvvm.

I have a grid and I populate it with data.

Upon the user double-clicking I want to get the row of data selected.

But it is not hitting my code.

My OnPropertyChamged method is not being hit.

I seem to be struggling learning these concepts.

can anyone point out my error please?

thanks

My mark-up:

<DataGrid Name="dgJobs" ItemsSource="{Binding}" AutoGenerateColumns="False" SelectionMode="Single"  >
    <DataGrid.InputBindings>
       <MouseBinding MouseAction="LeftDoubleClick"
             Command="{Binding Path=JobSearchCommand}"
             CommandParameter="{Binding ElementName=dgJobs,         
       Path=SelectedItem}"/>
    </DataGrid.InputBindings>
</DataGrid>

my code behind this mark-up:

public ucJobSearch()
{
    InitializeComponent();
    for (int index = 0; index < 300; index++)
    {
        ActiveState.JobSearchResults.Add(new CustomerJobs()
        {
            Add1 = "Add" + index.ToString(),
            FName = "Fname" + index.ToString(),
            SName = "Sname" + index.ToString(),
            Email = "Email" + index.ToString(),
            JobStatus = JobStatus.New
        });
    }
    dgJobs.ItemsSource = ActiveState.JobSearchResults;

    this.DataContext =new JobSearch();
}

my model:

public class CustomerJobs
{
    public int JobId { get; set; }     
    public string CustomerRef { get; set; }
    public string DateReq { get; set; }     
    public string JobRef { get; set; }      
    public JobStatus JobStatus { get; set; }
    public int CustomerId { get; set; }
    public string SName { get; set; }
    public string FName { get; set; }
    public string Add1 { get; set; }
    public string Town { get; set; }
    public string DOE { get; set; }
    public string Email { get; set; }
}

My Viewmodel:

public class JobSearch : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

My helper:

public class JobSearchCommand : ICommand
{
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        var job = parameter as CustomerJobs;
        var x = job.FName;
    }
}
Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179

1 Answers1

1

You need to have a property of type JobSearchCommand in the view model and then bind to it in XAML. You can change your view model class like this:

public class JobSearch : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public JobSearch()
    {
        JobSearchCommand = new JobSearchCommand();
    }

    public ICommand JobSearchCommand { get; private set; }

    public void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

}
chameleon
  • 984
  • 10
  • 15