2013-02-25 1 views
6

:RelayCommand CanExecute 동작

<Button x:Name="bOpenConnection" Content="Start Production" 
     Grid.Row="0" Grid.Column="0" 
     Height="30" Width="120" Margin="10" 
     HorizontalAlignment="Left" VerticalAlignment="Top" 
     Command="{Binding Path=StartProductionCommand}"/> 

StartProductionCommand = new RelayCommand(OpenConnection, CanStartProduction); 

private bool CanStartProduction() 
{ 
    return LogContent != null && !_simulationObject.Connected; 
} 

CanStartProduction 만 나는 다시 크기 UI 확인하고 즉시 업데이트되지 않습니다. 값을 변경할 때마다 업데이트되지 않는 이유는 무엇입니까?

답변

15

CommandManager 참조 명령이 LogContent_simulationObject.Connected에 의존한다는 것을 알 수있는 방법이 없습니다 참조 때문에 이러한 속성이 변경 될 때 자동으로 CanExecute을 재평가 할 수 없다.

CommandManager.InvalidateRequerySuggested을 호출하여 명시 적으로 재평가를 요청할 수 있습니다. 의 경우 CanExecute 모두를 다시 계산 함을 유의하십시오. 명령; 하나만 새로 고치려면 StartProductionCommand.RaiseCanExecuteChanged을 호출하여 명령 자체에서 CanExecuteChanged 이벤트를 발생시켜야합니다.

+0

예를 들어 주시거나이 링크를 사용하는 방법에 대한 링크를 게시 할 수 있습니까? 나는 이것을 어디에서 부르는가? – batmaci

+1

@batmaci, 커맨드의'CanExecute'를 재평가하고 싶을 때'CommandManager.InvalidateRequerySuggested'를 호출하면됩니다 –

관련 문제