2017-12-24 28 views
1

PropertyChanged는 항상 null이며 이름은 창에 표시되지 않습니다. 그것은 로마 인 것으로되어 있습니다. 왜 항상 Null을 표시하고 이름을 표시하지 않는지 알고 있습니까?왜 PropertyChanged가 항상 null입니까?

내 창 클래스

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 

    string _name; 
    public event PropertyChangedEventHandler PropertyChanged; 

    public string PersonName 
    { 
     get => _name; 
     set 
     { 
      _name = value; 
      OnPropertyChanged("PersonName"); 
     } 
    } 

    public MainWindow() 
    { 

     InitializeComponent(); 
     _name = "Adam"; 
     PersonName = "Roman"; 
    } 

    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
} 

내 XAML은

공공 public MainWindow()에서
<Grid> 
    <Label x:Name="Test" Width="100" Height="50" Foreground="Black" Content="{Binding PersonName, Mode=TwoWay}"></Label> 
</Grid> 
+0

등을 할 경우 XAML에서 DataContext를 설정 하시겠습니까? DataContext를 어딘가에 설정하지 않으면 바인딩이 작동하지 않습니다. – nemesv

+0

DataContext를 설정해야한다는 것을 알지 못했습니다. 이제는 감사합니다. –

+0

어쨌든 코드 뒤에 있다면,이 점이 무엇입니까? – FakeCaleb

답변

0

this.DataContext = this하고

가 보이는 작업을 추가

public MainWindow() 
    { 
     InitializeComponent(); 
     _name = "Adam"; 
     PersonName = "Roman"; 
     this.DataContext = this; 
    }