我在我的XAML中有一個DataTrigger
,它綁定到ViewModel類中的Property'ShowEffect'。我還有一個綁定到調用方法的RelayCommand
(下面顯示的類)的按鈕。在該方法中,我將「ShowEffect」設置爲true。但是,DataTrigger
似乎沒有迴應;沒有顯示的效果:從Action/Command設置數據上下文中的屬性
我宣佈使用屬性:
private Boolean _ShowEffect;
public Boolean ShowEffect
{
get { return _ShowEffect; }
set { _ShowEffect = value; }
}
RelayCommand
類:
public class RelayCommand : ICommand
{
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
}
我不知道這個問題是否與調度。任何人都可以建議爲什麼綁定不起作用,當我在該命令調用的方法中設置屬性?當我在ViewModel的其他地方設置屬性時,它工作。
如果您的虛擬機尚未實現INPC,並且ShowTeffect需要在更改屬性更改處理程序(在值更改時從它的設置程序更改)引發View更改以使其可以識別時,需要引發它。你現在得到的是一個簡單的屬性,它不會通知任何更新的視圖,這是很大的改變它的值時發生了什麼,但視圖永遠不知道有關的變化,你的'DataTrigger'似乎不工作。 – Viv
@Viv + 1啊,謝謝。在屬性設置器中添加'NotifyPropertyChanged(「ShowEffect」)'解決了它!你想添加一個答案,以便我可以接受看到,就好像你是第一個? – Andy
@Viv剛剛編輯以前的評論:) – Andy