2013-12-10 29 views
0

我有一些XAML更新屬性時,不同的屬性變化

<Button Background="{Binding ButtonBackground}" /> 
<Label Background="{Binding LabelBackground}" /> 

兩個應該在同一個屬性更改事件IsRunning 到目前爲止,我有三個屬性,ButtonBackgroundLabelBackgroundIsRunningIsRunning更新明確射擊全部三個都是OnNotifyPropertyChanged。如果我決定添加一個應該在同一個觸發器上更新的新屬性,這很乏味且容易出錯。

不同屬性發生更改時,是否可以指示數據綁定以獲取屬性的值?也許像<Button Background="{Binding ButtonBackground, Source=IsRunning} />

+0

這裏有幾種實現pubsub模式的方法!!還據我所知knockoutxaml還沒有退出:) – Daniel

+0

如果你沒有參數通知它會通知所有的。我通常添加一個方法NotitifyAll(),所以它在一個地方。 – Paparazzi

回答

1

如果您的IsRunning屬性是DependencyProperty,那麼您可以添加一個PropertyChangedCallback處理程序。該處理器將被調用每次在IsRunning屬性更新時間,這樣你就可以從那裏設置你的其他屬性:

public static readonly DependencyProperty IsRunningProperty = DependencyProperty. 
Register("IsRunning", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(false, 
OnIsRunningChanged)); 

public bool IsRunning 
{ 
    get { return (bool)GetValue(IsRunningProperty); } 
    set { SetValue(IsRunningProperty, value); } 
} 

private static void OnIsRunningChanged(DependencyObject d, 
DependencyPropertyChangedEventArgs e) 
{ 
    // Update your other properties here 
} 

如果它不是一個DependencyProperty,那麼你可以從模子更新您的其他屬性:

public bool IsRunning 
{ 
    get { return isRunning; } 
    set 
    { 
     isRunning = value; 
     NotifyPropertyChanged("IsRunning"); 
     // Update your other properties here 
    } 
} 
+0

換句話說,它是不可能的更新觸發器綁定到另一個屬性更新? – kasperhj