2013-09-22 56 views
0

我有自定義控件,它處理來自控件的事件並將其引發爲父項。MVVM事件和ICommand綁定

控制代碼

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爲空。

我該如何解決這個問題?

+1

我沒有看到任何代碼將'_strategy'分配給任何東西。我看到*會分配給它的自定義設置存取器,但我沒有看到任何所謂的。 –

回答

0

local:CommandBehavior。 命令 = 「{結合ThumbMoved}」

 <TimeTimeSlider:TimeSlider 
       StartDate="{Binding TimeLineStartDate}" 
       local:CommandBehavior.Event="ThumbMoved" 
       local:CommandBehavior.Command="{Binding ThumbMoved}" 
       local:CommandBehavior.CommandParameter="Thumb Place Ment Moved " 
           /> 
+0

這是一個答案或評論? –

0

你需要做你_strategy分配別的地方。在構造函數中,當發生另一個命令時,發生事件等 - 行爲不爲空的任何地方,例如實例化時或更改/更新時。現在,它永遠不會是set在那個setter中 - 可以通過在該setter中放置一個斷點來看待它。