我的双击绑定无法正常工作

时间:2016-04-19 12:42:14

标签: c# wpf mvvm

我有一个wpf c#app。 我正在尝试实现mvvm。

我有一个网格,我用数据填充它。

用户双击后我想要选择一行数据。

但它没有达到我的代码。

我的OnPropertyChamged方法没有被击中。

我似乎在努力学习这些概念。

有人可以指出我的错误吗?

感谢

我的加价:

<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>

这个标记后面的代码:

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();
}

我的模特:

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));
        }
    }
}

我的帮手:

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;
    }
}

1 个答案:

答案 0 :(得分:1)

您需要在视图模型中拥有JobSearchCommand类型的属性,然后在XAML中绑定它。您可以像这样更改视图模型类:

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));
        }
    }

}