我在这里慢慢给自己另一个头疼...... Greymatter工作...... :)
所以我在模型视图中有一个公共委托命令:
public class OCLMEditorModelView : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private readonly DelegateCommand<string> _DeleteStudentButtonClickCommand;
public ObservableCollection<Student> Students
{
get
{
return _Model.Students;
}
}
private Student _SelectedStudentItem;
public Student SelectedStudentItem
{
get { return _SelectedStudentItem; }
set
{
_SelectedStudentItem = value;
_EditStudentButtonClickCommand.RaiseCanExecuteChanged();
_DeleteStudentButtonClickCommand.RaiseCanExecuteChanged();
OnPropertyChanged("SelectedStudentItem");
}
}
private OCLMEditorModel _Model;
public OCLMEditorModelView()
{
_Model = new OCLMEditorModel();
_DeleteStudentButtonClickCommand = new DelegateCommand<string>(
(s) =>
{
String strMessage = String.Format(
Properties.Resources.AreYouSure, _SelectedStudentItem.Name);
if (AppMessageBox.Show(strMessage,
MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
_Model.Students.Remove(_SelectedStudentItem);
_Model.Serialize();
}
}, //Execute
(s) => _SelectedStudentItem != null); //CanExecute
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public DelegateCommand<string> DeleteStudentButtonClickCommand
{
get { return _DeleteStudentButtonClickCommand; }
}
}
当用户点击窗口上的删除按钮时,会处理上述内容:
<Button Content="Delete"
Margin="2"
Command="{Binding DeleteStudentButtonClickCommand}"/>
一切运作良好。所以,我决定我也想支持Delete键。我在互联网上看到了关于使用PreviewKeyDown
事件的足够信息。
所以,我改变了我的DataGrid
:
PreviewKeyDown="gridStudents_PreviewKeyDown"
处理程序:
private void gridStudents_PreviewKeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Delete)
{
if(gridStudents != null)
{
}
}
}
这是我感到困惑的地方。此时,当我检测到删除键时,我在代码后面。但我自己的删除事件处理程序位于模型视图中。我很感激我也可以复制代码来从这里进行删除。
但我试图找出如何再次使用我现有的命令处理程序。
所以,我试过了:
我以为我可以访问DataContext
并投射它然后调用它但是它会失败,你可以看到。
如何在两个地方使用相同的命令处理程序?从我的窗口上的删除按钮和删除键?
谢谢。
答案 0 :(得分:3)
有一个解决方案,无需代码。
您可以使用var abc =[];
var FansChart = [];
for (var i = 0; i < result.length; i++) {
for (var j = 0; j < data.result[0].trendData.length; j++) {
abc.push(data.result[i].trendData[j].value);
}
FansChart.push({
name : url[i],
data : abc
});
}
集合将errorCode: null,
message: "Success",
result: [{
urlId: "a279ab1177ab8d7011466e77b79b98f7",
trendData: [],
url: "https://www.instagram.com/p/BFQGk2WER56/?taken-by=fishfung_saimei&hl=en"
}, {
urlId: "a279ab1177ab8d7011466e77b79b98f7",
trendData: [{
date: "2016-06-04",
value: 0
}, {
date: "2016-06-21",
value: 0
}, {
date: "2016-06-12",
value: 0
}, {
date: "2016-06-05",
value: 0
}, {
date: "2016-06-07",
value: 0
}, {
date: "2016-06-19",
value: 0
}, {
date: "2016-06-25",
value: 1527
}, {
date: "2016-06-25",
value: 0
}, {
date: "2016-06-11",
value: 0
}, {
date: "2016-06-15",
value: 0
}, {
date: "2016-06-08",
value: 0
}, {
date: "2016-06-27",
value: 1527
}, {
date: "2016-06-27",
value: 0
}, {
date: "2016-06-06",
value: 0
}, {
date: "2016-06-29",
value: 0
}, {
date: "2016-06-09",
value: 0
}, {
date: "2016-06-03",
value: 0
}, {
date: "2016-06-17",
value: 0
}, {
date: "2016-06-24",
value: 1527
}, {
date: "2016-06-24",
value: 0
}, {
date: "2016-06-13",
value: 0
}, {
date: "2016-06-22",
value: 0
}, {
date: "2016-06-01",
value: 0
}, {
date: "2016-06-14",
value: 0
}, {
date: "2016-06-18",
value: 0
}, {
date: "2016-06-26",
value: 1527
}, {
date: "2016-06-26",
value: 0
}, {
date: "2016-06-20",
value: 0
}, {
date: "2016-06-10",
value: 0
}, {
date: "2016-06-23",
value: 0
}, {
date: "2016-06-28",
value: 0
}, {
date: "2016-06-02",
value: 0
}, {
date: "2016-06-16",
value: 0
}],
url: "https://www.instagram.com/p/BFQGk2WER56/?taken-by=fishfung_saimei&hl=en"
}, {
urlId: "a279ab1177ab8d7011466e77b79b98f7",
trendData: [{
date: "2016-06-04",
value: 0
}, {
date: "2016-06-21",
value: 0
}, {
date: "2016-06-12",
value: 0
}, {
date: "2016-06-05",
value: 0
}, {
date: "2016-06-07",
value: 0
}, {
date: "2016-06-19",
value: 0
}, {
date: "2016-06-25",
value: 1527
}, {
date: "2016-06-25",
value: 0
}, {
date: "2016-06-11",
value: 0
}, {
date: "2016-06-15",
value: 0
}, {
date: "2016-06-08",
value: 0
}, {
date: "2016-06-27",
value: 1527
}, {
date: "2016-06-27",
value: 0
}, {
date: "2016-06-06",
value: 0
}, {
date: "2016-06-29",
value: 0
}, {
date: "2016-06-09",
value: 0
}, {
date: "2016-06-03",
value: 0
}, {
date: "2016-06-17",
value: 0
}, {
date: "2016-06-24",
value: 1527
}, {
date: "2016-06-24",
value: 0
}, {
date: "2016-06-13",
value: 0
}, {
date: "2016-06-22",
value: 0
}, {
date: "2016-06-01",
value: 0
}, {
date: "2016-06-14",
value: 0
}, {
date: "2016-06-18",
value: 0
}, {
date: "2016-06-26",
value: 1527
}, {
date: "2016-06-26",
value: 0
}, {
date: "2016-06-20",
value: 0
}, {
date: "2016-06-10",
value: 0
}, {
date: "2016-06-23",
value: 0
}, {
date: "2016-06-28",
value: 0
}, {
date: "2016-06-02",
value: 0
}, {
date: "2016-06-16",
value: 0
}],
url: "https://www.instagram.com/p/BFQGk2WER56/?taken-by=fishfung_saimei&hl=en"
}],
绑定到同一个命令委托。
Key