简体   繁体   English

我的双击绑定不起作用

[英]My double-click binding is not working

I have a wpf c# app.我有一个 wpf c# 应用程序。 I am trying to implement mvvm.我正在尝试实现 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.我的 OnPropertyChamped 方法没有被命中。

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

You need to have a property of type JobSearchCommand in the view model and then bind to it in XAML.您需要在视图模型中有一个 JobSearchCommand 类型的属性,然后在 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));
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM