Я пытаюсь создать команду, которая наследуется от DependencyObject и ICommand. У меня есть следующий код:
public class CustomCommand : DependencyObject, ICommand
{
public static readonly DependencyProperty CommandProperty;
public static readonly DependencyProperty AfterCommandProperty;
static CustomCommand()
{
var ownerType = typeof(CustomCommand);
CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(Action), ownerType, new PropertyMetadata(null));
AfterCommandProperty = DependencyProperty.RegisterAttached("AfterCommand", typeof(Action), ownerType, new PropertyMetadata(null));
}
public Action Command
{
get => (Action)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
public Action AfterCommand
{
get => (Action)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
// Command & AfterCommand are always null
}
}
а также
<Button Content="Test">
<Button.Command>
<command:CustomCommand Command="{Binding Copy}" AfterCommand="{Binding AfterCopy}" />
</Button.Command>
</Button>
Когда я нажимаю кнопку «Тест», Command и AfterCommand имеют значение null. У вас есть идея? Каков наилучший способ, потому что я не могу добавить ссылку ICommand в свою ViewModel.
Спасибо
Command="{Binding Copy, PresentationTraceSources.TraceLevel=High}"
и посмотрите, что вы видите в выводе отладки. Я подозреваю, что вы обнаружите, что контекст отсутствует, потому что его нет в визуальном дереве. Вы можете попробовать создать свою команду в качестве ресурса и/или попробовать различные стратегии связывания прокси. - person 15ee8f99-57ff-4f92-890c-b56153   schedule 09.05.2017AfterCommand
: оно передаетCommandProperty
в GetValue/SetValue, а неAfterCommandProperty
. - person 15ee8f99-57ff-4f92-890c-b56153   schedule 09.05.2017