C# MVVM事件和ICommand绑定
我有一个自定义控件,它处理一个控件中的事件,并将其抛出给is parent 控制代码 我想将事件绑定到MVVM应用程序中的命令 视图代码 从名为CommandBehaviorBinding的类上的控件引发事件时C# MVVM事件和ICommand绑定,c#,mvvm,binding,controls,prism,C#,Mvvm,Binding,Controls,Prism,我有一个自定义控件,它处理一个控件中的事件,并将其抛出给is parent 控制代码 我想将事件绑定到MVVM应用程序中的命令 视图代码 从名为CommandBehaviorBinding的类上的控件引发事件时 public ICommand Command { get { return _command; } set { _command = value; //set the exec
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);
}
我得到一个“对象引用未设置为对象实例”错误,因为_策略为空
如何修复此问题?本地:CommandBehavior.命令=“{Binding ThumbMoved}”
您需要在其他地方执行策略任务。在构造函数中,当另一个命令发生时,事件发生,等等——只要行为不为null,例如实例化或更改/更新时。现在,在该setter中从未设置过
,可以通过在该setter中设置断点来查看它。我没有看到任何代码为\u策略分配任何内容。我看到一个自定义集访问器,它将分配给它,但我没有看到任何调用它的地方。这是一个答案还是一个注释?
<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;
}
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);
}
<TimeTimeSlider:TimeSlider
StartDate="{Binding TimeLineStartDate}"
local:CommandBehavior.Event="ThumbMoved"
local:CommandBehavior.Command="{Binding ThumbMoved}"
local:CommandBehavior.CommandParameter="Thumb Place Ment Moved "
/>