2009-09-04 166 views
3

我試圖創建一個情況,其中一個或兩個ToggleButton s可以在任何時間組。我遇到的問題是如果我更改UI狀態不更新的支持變量的狀態。WPF ToggleButton IsChecked綁定問題

我確實有INotifyPropertyChanged實施。

我已經建立了我ToggleButton就像這樣:

 <ToggleButton IsChecked="{Binding Path=IsPermanentFailureState, Mode=TwoWay}" 
         HorizontalContentAlignment="Center" 
         VerticalContentAlignment="Center"> 
      <TextBlock TextWrapping="Wrap" 
         TextAlignment="Center">Permanent Failure</TextBlock> 
     </ToggleButton> 
     <ToggleButton IsChecked="{Binding Path=IsTransitoryFailureState, Mode=TwoWay}" 
         HorizontalContentAlignment="Center" 
         VerticalContentAlignment="Center"> 
      <TextBlock TextWrapping="Wrap" 
         TextAlignment="Center">Temporary Failure</TextBlock> 
     </ToggleButton> 

這是我的後盾屬性(我使用的MVVM模式,其他的綁定工作,在ToggleButton IE點擊確實進入這些屬性設置。當我改變通過代碼的狀態下,切換按鈕不改變視覺狀態,IE我設置爲後盾屬性設置爲false,但按鈕撐檢查。

public bool? IsPermanentFailureState 
    { 
     get { return isPermFailure; } 
     set 
     { 
      if (isPermFailure != value.Value) 
      { 
       NotifyPropertyChanged("IsPermanentFailureState"); 
      } 
      isPermFailure = value.Value; 
      if (isPermFailure) IsTransitoryFailureState = false; 
     } 
    } 

    public bool? IsTransitoryFailureState 
    { 
     get { return isTransitoryFailureState; } 
     set 
     { 
      if (isTransitoryFailureState != value.Value) 
      { 
       NotifyPropertyChanged("IsTransitoryFailureState"); 
      } 
      isTransitoryFailureState = value.Value; 
      if (isTransitoryFailureState) IsPermanentFailureState = false; 
     } 
    } 

回答

8

的問題就是你」重新提高財產變更通知在實際改變財產價值之前。因此,WPF讀取該屬性的舊值,而不是新值。改成這樣:

public bool? IsPermanentFailureState 
{ 
    get { return isPermFailure; } 
    set 
    { 
     if (isPermFailure != value.Value) 
     { 
      isPermFailure = value.Value; 
      NotifyPropertyChanged("IsPermanentFailureState"); 
     } 
     if (isPermFailure) IsTransitoryFailureState = false; 
    } 
} 

public bool? IsTransitoryFailureState 
{ 
    get { return isTransitoryFailureState; } 
    set 
    { 
     if (isTransitoryFailureState != value.Value) 
     { 
      isTransitoryFailureState = value.Value; 
      NotifyPropertyChanged("IsTransitoryFailureState"); 
     } 
     if (isTransitoryFailureState) IsPermanentFailureState = false; 
    } 
} 

順便說一句,你說,當你使用的接口,而不是代碼它的工作原理,但我不能看到它可能會。

+0

你打敗了我肯特。從我身上向上一箭。 – 2009-09-04 12:32:55

+1

哇 - 我不應該是這個笨蛋!這可能是你坐下來編碼的時候發生的事情。謝謝! – 2009-09-04 13:08:31

0

您的代碼看起來不正確:您在更改之前通知您。我認爲你需要移動你的 isPermFailure = value.Value; 裏面:

 if (isPermFailure != value.Value)    
     { 
      isPermFailure = value.Value; 
      NotifyPropertyChanged("IsPermanentFailureState"); 
     } 

和同樣的另一個。

我想你會想移動的其它陳述的在那裏,太:

 if (isPermFailure != value.Value)    
     { 
      isPermFailure = value.Value; 
      NotifyPropertyChanged("IsPermanentFailureState"); 
      if (isPermFailure) 
       IsTransitoryFailureState = false; 
     } 

否則你會被設置狀態,並通知在那一個,不必要的。

+1

關於你最後的評論:不,它不會不必要地通知。它所要做的就是不必要地調用該屬性。如果底層字段發生了變化,setter邏輯只會導致通知。 – 2009-09-04 12:46:13

+1

你是對的:不小心選擇了詞語,但我仍然認爲我的答案更好。 :-) – serialhobbyist 2009-09-04 16:18:18