0

I have a user control and within I defined the following property:

public string MyProperty
{
      get { return (string)GetValue(MyPropertyProperty); }
      set { SetValue(MyPropertyProperty, value); }
}
        
public static readonly DependencyProperty MyPropertyProperty =
      DependencyProperty.Register("MyProperty", typeof(string),
           typeof(MyUserControlView), new PropertyMetadata(null, MyPropertyChangedCallback));

private static void MyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
     var control = (MyUserControl) d;
     control.MyProperty = (string)e.NewValue;
}

and the following command:

public ICommand MyCommand
{
     get { return (ICommand)GetValue(MyCommandProperty); }
     set { SetValue(MyCommandProperty, value); }
}

public static readonly DependencyProperty MyCommandProperty =
     DependencyProperty.Register("MyCommand", typeof(ICommand), typeof(MyUserControlView),
         new PropertyMetadata(null, MyCommandPropertyChangedCallback));

private static void MyCommandPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
     var control = (MyUserControlView)d;
     control.MyCommand = (DelegateCommand)e.NewValue;
}

void ExecuteMyCommand() {...}

bool CanExecuteMyCommand(){
    return !string.IsNullOrWhiteSpace(MyProperty);
}

I Initialize this command with

SetValue(MyCommandProperty, new DelegateCommand(ExecuteMyCommand, CanExecuteMyCommand));

The problem is that the CanExecuteMyCommand() method does not work, because it does not use the current value of MyProperty. Why is this and how can I use my command correctly in a user control?

Anno
  • 761
  • 1
  • 10
  • 22

1 Answers1

0

You have to invoke CanExecuteChanged after changing MyProperty it will call CanExecute.

GeorgeKarlinzer
  • 311
  • 2
  • 9