2014-02-19 16 views
1

什麼,我想實現的是,當被選中該複選框,文本框會顯示True,則該複選框取消選中,文本框將顯示假。綁定問題 - 價值不更新上查看

目前最大的問題是,最初它的工作原理,該複選框將被選中,並且文本框會顯示正確的。但是,當我取消選中它時,文本框仍然顯示爲True。

對不起,如果我失去了一些東西簡單,仍然試圖掌握WPF和C#。

感謝您的幫助。

的XAML:

<Grid> 
<CheckBox Content="Check me" HorizontalAlignment="Left" Height="19" Margin="88,63,0,0" VerticalAlignment="Top" Width="86" IsChecked="{Binding CurrentValue, Mode= TwoWay}"/> 
<TextBlock Text="{Binding CurrentValue, Mode=TwoWay}" HorizontalAlignment="Left" Height="39" Margin="88,82,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="86"/> 
</Grid> 

主窗口:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = new MainView(); 
    } 

} 

的MainView:

public class MainView : INotifyPropertyChanged 
    { 

    private bool currentValue = true; 
    public event PropertyChangedEventHandler PropertyChanged; 

    public bool CurrentValue 
    { 
     get 
     { 
      return currentValue; 
     } 
     set 
     { 
      if (currentValue == value) 
      { 
       return; 
      } 
       currentValue = value; 
       RaisePropertyChange("Check_Value"); 

     } 
    } 



    protected void RaisePropertyChange(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

    } 

} 

回答

2

你設置你的財產錯誤下面你如何解決這個正確

public bool CurrentValue 
    { 
     get 
     { 
      return currentValue; 
     } 
     set 
     { 
      if (currentValue != value) 
      {  
       currentValue = value; 
      RaisePropertyChange("CurrentValue"); 
      } 



    } 
} 
+0

這解決了它。我確定我已經試過了。謝謝。 – user2469515

1

而不是

RaisePropertyChange("Check_Value"); 

嘗試

RaisePropertyChange("CurrentValue"); 

值是通過必須與您的屬性名稱

+0

感謝您的幫助 – user2469515

+0

當你引發你傳遞的'INotifyPropertyChanged.PropertyChanged'事件屬性名稱必須與你想要刷新的視圖模型中的名稱完全相同。這也是區分大小寫的,所以'CurrentValue'與例如'currentvalue'不一樣。 – dkozl