2013-08-06 2 views
3

TextBox의 Text 속성을 ViewModel 속성의 자식으로 바인딩하고 싶습니다.클래스의 속성에 컨트롤 속성을 바인딩합니다. ViewModel


foo.cs :

public class foo() 
    { 
     public foo() 
     { 
     Bar = "Hello World"; 
     } 

     public string Bar { Get; private Set;} 
     //Some functions 
    } 

ViewModel.cs :

public class ViewModel : INotifyPropertyChanged 
    { 
     public foo Property { Get; Set; } 

     //some more properties 

     public ViewModel() 
     { 

     } 

     public event PropertyChangedEventHandler PropertyChanged;   
     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));    
     } 
    } 

Window.xaml.cs :

public ViewModel MyViewModel { get; set; } 

    public Window() 
    { 
     MyViewModel = new ViewModel(); 
     this.DataContext = MyViewModel; 
     MyViewModel.Property = new foo(); 
    } 
다음

내 코드입니다

Window.xaml는 :

<!-- 
    Some controls 
    --> 

    <TextBox Text="{Binding Path=Property.Bar}"></TextBox> 

또한 thisthis을 시도했지만 그들 중 누구도 나를 위해 일하지 않는다.

답변

5

당신은 당신의 ViewModelINotifyPropertyChanged을 구현했지만 Property가 변경 될 때 당신이 그것을 호출하지

시도 :

public class ViewModel : INotifyPropertyChanged 
{ 
    private foo _property; 
    public foo Property 
    { 
    get{ return _property; } 
    set{ _property = value; OnPropertyChanged(); } 
    } 

    ................. 
관련 문제