委托命令不在属性更改上执行

时间:2016-06-09 18:11:21

标签: mvvm prism delegatecommand

我目前正在尝试在ViewA中创建一个滑块来更改viewA和viewB中文本的字体大小。我已经正确绑定了所有内容,但是当更改字体大小属性时,委托命令不会调用execute方法。如果我手动调用此函数,一切都按预期工作,因此可能是一行代码问题。 ViewAViewModel如下:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

为什么会这样?您没有在Font属性的setter中调用UpdateSizeCommand.Execute。除非您将命令绑定到命令属性或手动调用它,否则不会调用该命令。