2012-11-13 2 views
0

있습니다윈도우 스토어 나는 윈도우 스토어 프로젝트를 바인딩 문제

다음과 같이 내 바인딩
class MyModel 
{ 
    private int _testVar; 
    public int TestVariable 
    { 
     get { return _testVar; } 
     set 
     { 
      _testVar = value; 
      NotifyPropertyChanged("TestVariable"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(string property) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
    } 


} 

은 다음과 같습니다

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> 
    <TextBlock Text="{Binding Path=TestVariable}" /> 
    <Button Click="Button_Click_1"></Button> 
</Grid> 

그리고 뒤에있는 코드 :이로

MyModel thisModel = new MyModel(); 

    public MainPage() 
    { 
     this.InitializeComponent(); 

     thisModel.TestVariable = 0; 
     DataContext = thisModel; 
    } 

을 포인트, 바인딩은 내가 0을 보여주는 텍스트 블록을 얻는 것처럼 보입니다. 그러나, 다음과 같이 버튼 클릭 이벤트를 처리 할 때 :

private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     thisModel.TestVariable++; 
    } 

숫자가 증가하지 않습니다. 내가 여기서 무엇을 놓치고 있니?

답변

2

클래스에 INotifyPropertyChanged이 구현되어 있지 않은 것으로 보입니다. 은 내가 class MyModel : INotifyPropertyChanged

+0

내가 그것을 놓쳤다 고 생각할 수 없다! –

1

먼저, 뷰 모델에서 INotifyPropertyChanged를 구현하거나 더 나은 MVVM Light 같은 MVVM 도서관의 어떤 종류를 사용해야 할 전망 말은, 이것은 당신에게 많은 도움이 될 것입니다.
둘째, thisModel.TestVariable ++가 실제로 값을 업데이트하는 경우 확실하지 않습니다. thisModel.TestVariable = thisModel.TestVariable + 1;을 사용하십시오.

+0

++에서 PropertyChanged가 발생한다는 것은 의심의 여지가 없습니다. – Nagg

+0

INotifyPropertyChnaged를 구현하면 thisModel.TestVariable ++가 제대로 작동하는지 확인할 수 있습니다. –