Я только начал писать свой собственный редактор диаграмм состояния. Я хотел, чтобы это было в шаблоне MVVM. Но у меня возникают проблемы с привязкой событий к командам в viewmodel.Привязка события к команде в режиме просмотра
При запуске приложения появляется это InvalidCastException.
System.InvalidCastException: Невозможно наложить объект типа «System.Reflection.RuntimeEventInfo» на тип «System.Reflection.MethodInfo».
У меня есть следующий фрагмент в моем файле xaml.
<UserControl.Resources>
<vm:StateViewModel x:Key="StateViewModel"/>
</UserControl.Resources>
<Grid DataContext="{StaticResource StateViewModel}">
<Rectangle MouseLeftButtonDown="{Binding Path=DragStartCommand}">
</Grid>
В моей модели StateViewModel я создал свойство ICommand.
private DelegateCommand _dragStartCommand;
public ICommand DragStartCommand
{
get
{
if (_dragStartCommand == null)
{
_dragStartCommand = new DelegateCommand(StartDragging);
}
return _dragStartCommand;
}
}
private void StartDragging(object obj)
{
// ...
}
и класс DelegateCommand выглядит следующим образом
public class DelegateCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public event EventHandler CanExecuteChanged;
public DelegateCommand(Action<object> execute)
: this(execute, null)
{
}
public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
}
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
Я надеюсь, что вы можете помочь мне решить эту проблему. Для этого было бы неплохо узнать, как я могу передать EventArgs в свою команду и использовать их.
где О Нас имеет место ошибка, на какой линии? –
это происходит в корневом теге моего UserControl – Zarzan