我有自定义控件,它处理来自控件的事件并将其抛出给父级。
控制代码
public delegate void ThumbMovedEventHandler(object sender);
public event ThumbMovedEventHandler ThumbMoved;
private void SliderTimeLine_OnDragCompleted(object sender, DragCompletedEventArgs e)
{
if (ThumbMoved != null)
ThumbMoved(this);
}
我想将事件绑定到我的 MVVM 应用程序中的命令。
查看代码
<TimeTimeSlider:TimeSlider
StartDate="{Binding TimeLineStartDate}"
local:CommandBehavior.Event="ThumbMoved"
local:CommandBehavior.Action="{Binding ThumbMoved}"
local:CommandBehavior.CommandParameter="Thumb Place Ment Moved "
/>
视图模型代码
private ICommand thumbMoverCommand;
public ICommand ThumbMoved
{
get { return this.thumbMoverCommand ?? (this.thumbMoverCommand = new DelegateCommand(this.ExcuteThumbMoved)); }
}
public void ExcuteThumbMoved()
{
//Do Something;
}
当从名为 CommandBehaviorBinding 的类上的控件中引发事件时
public ICommand Command
{
get { return _command; }
set
{
_command = value;
//set the execution strategy to execute the command
_strategy = new CommandExecutionStrategy { Behavior = this };
}
}
public void Execute()
{
_strategy.Execute(CommandParameter);
}
我收到“对象引用未设置为对象的实例”错误,因为 _strategy 为空。
我该如何解决?