2016-06-09 39 views
1

我目前正在嘗試使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; 
    } 
} 
+0

您的綁定可能不正確。使用像Snoop這樣的工具在運行時檢查綁定。 – Will

回答

0

爲什麼會這樣?您沒有在Font屬性的設置器中調用UpdateSizeCommand.Execute。除非將其綁定到命令屬性或手動調用該命令,否則該命令將不會被調用。

+0

哦,我現在看到。我期望執行方法在觀察屬性發生變化時被調用。 – Methalous