mvvmprismdelegatecommand

Delegate command not executing on property change


I am currently trying to make a slider in ViewA change the font size of text in viewA and viewB. I have everything bound correctly, but the delegate command is not calling the execute method when the font size property is changed. If I manually call this function everything works as expected, so it is likely one line of code that is the problem. The ViewAViewModel is below:

public class ViewAViewModel : BindableBase
{
    private Person _CoolChick = new Person();
    private int _fontSize = 12;
    private IEventAggregator _eventAggregator;
    public DelegateCommand UpdateSizeCommand { get; set; }

    public Person CoolChick
    {
        get
        {
            return _CoolChick;
        }
        set
        {
            SetProperty(ref _CoolChick, value);
        }
    }

    public int FontSize
    {
        get { return _fontSize; }
        set {
            Console.WriteLine(_fontSize + " => Font Size");
            SetProperty(ref _fontSize, value);
            //Execute();
        }
    }

    public ViewAViewModel(IEventAggregator eventAggregator)
    {
        CoolChick.Age = 25;
        CoolChick.Name = "Methalous";
        _eventAggregator = eventAggregator;

        //likely the problem in this code
        UpdateSizeCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => FontSize);

    }

    private void Execute()
    {
        _eventAggregator.GetEvent<UpdateEvent>().Publish(FontSize);
    }

    private bool CanExecute()
    {
        return true;
    }
}

Solution

  • Why would it? You're not calling UpdateSizeCommand.Execute in the setter of your Font property. The command will not invoke unless you bind it to a command property or invoke it manually.