CommandReference 클래스는 현재 참조 할 수있는 어셈블리에 포함되어 있지 않지만 M-V-VM 프로젝트 템플릿에 포함되어 있습니다. 따라서 템플릿에서 응용 프로그램을 빌드하지 않으면 다른 곳에서 클래스를 가져와야합니다. 샘플 프로젝트에서 복사하기로했습니다. 모든 사람들이이 작은 덩어리에 쉽게 액세스 할 수 있도록하기 위해 아래에 포함 시켰지만 향후 버전의 M-V-VM Toolkit에서 템플릿에 대한 업데이트를 확인하십시오.
/// <summary>
/// This class facilitates associating a key binding in XAML markup to a command
/// defined in a View Model by exposing a Command dependency property.
/// The class derives from Freezable to work around a limitation in WPF when data-binding from XAML.
/// </summary>
public class CommandReference : Freezable, ICommand
{
public CommandReference()
{
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandReference), new PropertyMetadata(new PropertyChangedCallback(OnCommandChanged)));
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
#region ICommand Members
public bool CanExecute(object parameter)
{
if (Command != null)
return Command.CanExecute(parameter);
return false;
}
public void Execute(object parameter)
{
Command.Execute(parameter);
}
public event EventHandler CanExecuteChanged;
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CommandReference commandReference = d as CommandReference;
if (commandReference != null)
{
ICommand oldCommand = e.OldValue as ICommand;
if (oldCommand != null)
oldCommand.CanExecuteChanged -= commandReference.CanExecuteChanged;
ICommand newCommand = e.NewValue as ICommand;
if (newCommand != null)
newCommand.CanExecuteChanged += commandReference.CanExecuteChanged;
}
}
#endregion
#region Freezable
protected override Freezable CreateInstanceCore()
{
return new CommandReference();
}
#endregion
}
즐기십시오!
J, Silverlight에서 Freezable을 찾을 수 없습니다. 무엇이 없습니까? – kenny