저는 Silverlight가 새로 도입되었으며 명령과 관련된 몇 가지 문제가 있습니다. 내 ViewModel에서 ObservableCollection에 바인딩 된 DataGrid가 있습니다. 나 또한 버튼을 가지고있다.Silverlight에서 SelectedItem이 변경되고 명령이 변경되었습니다.
<Button Command="{Binding AddCommand}">Add</Button>
Command 속성이 ViewModel의 명령에 바인딩되어있다. 그
public class GenericCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private Action<object> execute;
private Func<object, bool> canExecute;
private bool previousState;
public GenericCommand(Action<object> execute, Func<object, bool> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (canExecute == null) return false;
bool currentState = canExecute(parameter);
if (currentState != previousState)
{
previousState = currentState;
if (CanExecuteChanged != null)
CanExecuteChanged(this, new EventArgs());
return currentState;
}
return currentState;
}
public void Execute(object parameter)
{
if (execute == null) return;
execute(parameter);
}
}
속성 AddCommand이 방법
AddCommand = new GenericCommand(Add,CanAdd);
public bool CanAdd(object param)
{
return SelectedItem != null;
}
public void Add(object param)
{
}
을 만든 것처럼 명령 클래스 문제는이 CommandBinding을이의 selectedItem의 변화에 반응하지 않는 것으로 보인다이다 보인다. 내 응용 프로그램을 실행하면 격자 행이 선택되지 않고 CanAdd 함수가 호출 된 것을 볼 수 있습니다. 그러나 일부 항목을 클릭하면 CanAdd 함수가 호출되지 않습니다 - 뷰 모델의 SelectedItem 속성이 변경되었음을 알 수 있다는 사실에도 불구하고 ?? 내가 뭘 잘못 했니? 외부 라이브러리를 사용하지 않고 명령을 사용할 수 있습니까? 내가 GeneriCommand 클래스 그러나 WPF에서 WPF에서 유사한 코드를 작성하는 데 사용 나는
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
을 사용하는 문제는 실버 라이트에서 CommandManager의 같은 같은 것은 없다는 것입니다.