[英]My double-click binding is not working
我有一个 wpf c# 应用程序。 我正在尝试实现 mvvm。
我有一个网格,并用数据填充它。
用户双击后,我想选择数据行。
但它没有击中我的代码。
我的 OnPropertyChamped 方法没有被命中。
我似乎在努力学习这些概念。
谁能指出我的错误?
谢谢
我的标记:
<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; }
}
我的视图模型:
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;
}
}
您需要在视图模型中有一个 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));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.